diff --git a/editor/src/sr.js b/editor/src/sr.js new file mode 100644 index 0000000..d98fe9f --- /dev/null +++ b/editor/src/sr.js @@ -0,0 +1,131 @@ +import $ from 'jquery' +import search from "./search"; +import qs from "querystring"; + +function fillResult($modal, result) { + $modal.data('block-id', result.id) + $modal.data('block-original-text', result.line) + $modal.find('.block-title').show().text(result.title) + $modal.find('.block-text').show().val(result.line) +} + +$(function () { + let reviewBlocks = []; + + $('.start-review').on('click', function () { + search.startQuery('+tag:review +tag:sr/1', {internal: false}).then((results) => { + reviewBlocks = results + + $('.review-modal .end-of-review').hide(); + $('.review-modal .review').show(); + + let $modal = $('.review-modal').first() + $modal.addClass('is-active') + + if (reviewBlocks.length > 0) { + const first = reviewBlocks.shift() + fillResult($modal, first) + } else { + $('.review-modal .block-text').hide(); + $('.review-modal .block-title').hide(); + $('.review-modal .end-of-review').show(); + $('.review-modal .review').hide(); + } + }) + + return false + }); + + $('.modal-close, .delete, .close').on('click', function () { + $(this).parents('.modal').removeClass('is-active') + window.location.reload() + }); + + $('.modal .review').on('click', function () { + const $modal = $(this).parents('.modal') + const review = $(this).data('review') + + // NOTE: should we keep the review and sr/* tag in the editable text? + const originalText = $modal.data('block-original-text') + let text = $modal.find('.block-text').val() + let id = $modal.data('block-id') + + // let oldText = text + // .replace(/#\[\[review\]\]/, '') + // .replace(/#\[\[sr\/(\d+)\]\]/g, '') + // .trim() + + processText(review, text, originalText, id) + .then(() => { + if (reviewBlocks.length > 0) { + const first = reviewBlocks.shift() + // reload note with id + search.startQuery('id:' + first.id, {internal: false}).then((results) => { + fillResult($modal, results[0]) + }) + } else { + $('.review-modal .block-text').hide(); + $('.review-modal .block-title').hide(); + $('.review-modal .end-of-review').show(); + $('.review-modal .review').hide(); + reviewBlocks = [] + } + }).catch(e => console.log(e)) + }); + + function processText(review, text, originalText, id) { + const changed = text !== originalText + if (changed) { + let newText = text.replace(/#\[\[sr\/(\d+)\]\]/, '#[[sr/1]]'); + return fetch('/api/block/append', { + method: 'post', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + body: qs.encode({id, text: newText}), + }).then(() => { + let oldText = originalText + .replace(/#\[\[sr\/(\d+)\]\]/, function (text, srCount) { + if (review === 'never') return ''; + let nextCount = 1; + const count = parseInt(srCount) + if (review === 'again') nextCount = 1 + if (review === 'soon') nextCount = count + if (review === 'later') nextCount = count + 1 + return '#[[sr/' + nextCount + ']]'; + }).trim() + if (review === 'never') oldText = oldText.replace(/#\[\[review\]\]/, '') + return fetch('/api/block/replace', { + method: 'post', + headers: {'Content-Type': 'application/x-www-form-urlencoded'}, + body: qs.encode({id, text: oldText}), + }).then(() => { + return text + }) + }) + } else { + let oldText = text + .replace(/#\[\[sr\/(\d+)\]\]/, function (text, srCount) { + if (review === 'never') return ''; + + let nextCount = 1; + const count = parseInt(srCount) + if (review === 'again') nextCount = 1 + if (review === 'soon') nextCount = count + if (review === 'later') nextCount = count + 1 + return '#[[sr/' + nextCount + ']]'; + }).trim() + if (review === 'never') oldText = oldText.replace(/#\[\[review\]\]/, '') + + return fetch('/api/block/replace', { + method: 'post', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + body: qs.encode({id, text: oldText}), + }).then(() => { + return text + }) + } + } +});