Solution: add sr.js
This commit is contained in:
parent
b4a721ffeb
commit
40dfa46d7c
131
editor/src/sr.js
Normal file
131
editor/src/sr.js
Normal file
|
@ -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
|
||||
})
|
||||
}
|
||||
}
|
||||
});
|
Loading…
Reference in New Issue
Block a user