Use CodeMirror editor

This commit is contained in:
2026-06-05 10:10:58 +02:00
parent 4e24f876c9
commit de3abed6d7
15 changed files with 1207 additions and 283 deletions
+13 -19
View File
@@ -48,26 +48,20 @@ window.EditorMovie = (function () {
return out.join('\n');
}
function replaceRange(ta, start, end, text) {
ta.focus();
ta.selectionStart = start;
ta.selectionEnd = end;
document.execCommand('insertText', false, text);
}
function insertOrReplace(ta, markup) {
var t = ta.value || '';
// ctx is the CM adapter from main.js: { getValue(), replace(start,end,text) }.
function insertOrReplace(ctx, markup) {
var t = ctx.getValue() || '';
var b = t.indexOf(BEGIN);
var e = t.indexOf(END);
if (b !== -1 && e !== -1 && e > b) {
replaceRange(ta, b, e + END.length, markup);
ctx.replace(b, e + END.length, markup);
} else {
var h = t.match(/^#{1,6}\s+.+?\s*$/m);
if (h) {
var idx = t.indexOf(h[0]) + h[0].length;
replaceRange(ta, idx, idx, '\n\n' + markup);
ctx.replace(idx, idx, '\n\n' + markup);
} else {
replaceRange(ta, 0, 0, t ? markup + '\n\n' : markup);
ctx.replace(0, 0, t ? markup + '\n\n' : markup);
}
}
}
@@ -125,7 +119,7 @@ window.EditorMovie = (function () {
});
}
function importWithKey(textarea, key, initialTitle) {
function importWithKey(ctx, key, initialTitle) {
var input = document.createElement('input');
input.type = 'text';
input.className = 'input';
@@ -148,7 +142,7 @@ window.EditorMovie = (function () {
data.Error === 'Invalid API key!') {
localStorage.removeItem(STORAGE_KEY);
promptForKey(true, function (newKey) {
importWithKey(textarea, newKey, raw);
importWithKey(ctx, newKey, raw);
});
return;
}
@@ -157,7 +151,7 @@ window.EditorMovie = (function () {
(data && data.Error) || 'Movie not found.');
return;
}
insertOrReplace(textarea, buildBlock(data));
insertOrReplace(ctx, buildBlock(data));
})
.catch(function () {
showMessage('Import failed', 'OMDb lookup failed.');
@@ -167,16 +161,16 @@ window.EditorMovie = (function () {
});
}
function run(textarea) {
var initialTitle = firstHeading(textarea.value || '');
function run(ctx) {
var initialTitle = firstHeading(ctx.getValue() || '');
var key = localStorage.getItem(STORAGE_KEY);
if (!key) {
promptForKey(false, function (newKey) {
importWithKey(textarea, newKey, initialTitle);
importWithKey(ctx, newKey, initialTitle);
});
return;
}
importWithKey(textarea, key, initialTitle);
importWithKey(ctx, key, initialTitle);
}
return { run: run };