Problem: sr.js is messy
Solution: cleanup sr.js and extract functions
This commit is contained in:
parent
d584fe8bf7
commit
8fa7d4170f
109
editor/src/sr.js
109
editor/src/sr.js
|
@ -9,6 +9,46 @@ function fillResult($modal, result) {
|
||||||
$modal.find('.block-text').show().val(result.line)
|
$modal.find('.block-text').show().val(result.line)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function replaceBlock(id, oldText) {
|
||||||
|
return fetch('/api/block/replace', {
|
||||||
|
method: 'post',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
|
},
|
||||||
|
body: qs.encode({id, text: oldText}),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function appendBlock(id, newText) {
|
||||||
|
return fetch('/api/block/append', {
|
||||||
|
method: 'post',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
|
},
|
||||||
|
body: qs.encode({id, text: newText}),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function resetSRBox(text) {
|
||||||
|
let newText = text.replace(/#(?:\[\[)?sr\/(\d+)(?:\]\])?/, '#sr/1');
|
||||||
|
return newText;
|
||||||
|
}
|
||||||
|
|
||||||
|
function processSRBox(review, originalText) {
|
||||||
|
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') return oldText.replace(/#(?:\[\[)?review(?:\]\])?/, '')
|
||||||
|
return oldText
|
||||||
|
}
|
||||||
|
|
||||||
$(function () {
|
$(function () {
|
||||||
let reviewBlocks = [];
|
let reviewBlocks = [];
|
||||||
|
|
||||||
|
@ -50,19 +90,15 @@ $(function () {
|
||||||
let text = $modal.find('.block-text').val()
|
let text = $modal.find('.block-text').val()
|
||||||
let id = $modal.data('block-id')
|
let id = $modal.data('block-id')
|
||||||
|
|
||||||
// let oldText = text
|
|
||||||
// .replace(/#\[\[review\]\]/, '')
|
|
||||||
// .replace(/#\[\[sr\/(\d+)\]\]/g, '')
|
|
||||||
// .trim()
|
|
||||||
|
|
||||||
processText(review, text, originalText, id)
|
processText(review, text, originalText, id)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
if (reviewBlocks.length > 0) {
|
if (reviewBlocks.length > 0) {
|
||||||
const first = reviewBlocks.shift()
|
const first = reviewBlocks.shift()
|
||||||
// reload note with id
|
// reload note with id
|
||||||
search.startQuery('id:' + first.id, {internal: false}).then((results) => {
|
search.startQuery('id:' + first.id, {internal: false})
|
||||||
fillResult($modal, results[0])
|
.then((results) => {
|
||||||
})
|
fillResult($modal, results[0])
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
$('.review-modal .block-text').hide();
|
$('.review-modal .block-text').hide();
|
||||||
$('.review-modal .block-title').hide();
|
$('.review-modal .block-title').hide();
|
||||||
|
@ -73,59 +109,10 @@ $(function () {
|
||||||
}).catch(e => console.log(e))
|
}).catch(e => console.log(e))
|
||||||
});
|
});
|
||||||
|
|
||||||
function processText(review, text, originalText, id) {
|
async function processText(review, text, originalText, id) {
|
||||||
const changed = text !== originalText
|
if (text !== originalText) {
|
||||||
if (changed) {
|
await appendBlock(id, resetSRBox(text))
|
||||||
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
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
await replaceBlock(id, processSRBox(review, originalText))
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue
Block a user