1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
function setReadOnly(editor, readOnlyLines) { const session = editor.session; const Range = editor; const readOnlyRanges = []; for (let i = 0; i < readOnlyLines.length; i += 1) { const newRange = [readOnlyLines[i] - 1, 0, readOnlyLines[i], 0]; readOnlyRanges.push({ start: { row: readOnlyLines[i] - 1, column: 0, }, end: { row: readOnlyLines[i] - 1, column: 1000, }, }); } function before(obj, method, wrapper) { const orig = obj[method]; obj[method] = function (...arg) { const args = Array.prototype.slice.call(arg); return wrapper.call(this, () => orig.apply(obj, args), args); }; return obj[method]; }
function intersects(range) { return editor.getSelectionRange().intersects(range); } function preventReadonly(next, args) { for (let i = 0; i < readOnlyRanges.length; i += 1) { if (intersects(readOnlyRanges[i])) return; } next(); }
function intersectsRange(newRange) { for (let i = 0; i < readOnlyRanges.length; i += 1) { if (newRange.intersects(readOnlyRanges[i])) return true; } return false; }
function onEnd(position) { const row = position.row; const column = position.column; for (let i = 0; i < readOnlyRanges.length; i += 1) { if (readOnlyRanges[i].end.row === row && readOnlyRanges[i].end.column === column) return true; } return false; }
function outSideRange(position) { const row = position.row; const column = position.column; for (let i = 0; i < readOnlyRanges.length; i += 1) { if (readOnlyRanges[i].start.row < row && readOnlyRanges[i].end.row > row) { return false; } if (readOnlyRanges[i].start.row === row && readOnlyRanges[i].start.column < column) { if (readOnlyRanges[i].end.row !== row || readOnlyRanges[i].end.column > column) { return false; } } else if (readOnlyRanges[i].end.row === row && readOnlyRanges[i].end.column > column) { return false; } } return true; } editor.keyBinding.addKeyboardHandler({ handleKeyboard(data, hash, keyString, keyCode, event) { if (Math.abs(keyCode) === 13 && onEnd(editor.getCursorPosition())) { return false; } if (hash === -1 || (keyCode <= 40 && keyCode >= 37)) return false; for (let i = 0; i < readOnlyRanges.length; i += 1) { if (intersects(readOnlyRanges[i])) { return { command: "null", passEvent: false }; } } }, }); before(editor, "onPaste", preventReadonly); before(editor, "onCut", preventReadonly);
const old$tryReplace = editor.$tryReplace; editor.$tryReplace = function (range, replacement) { return intersectsRange(range) ? null : old$tryReplace.apply(this, arguments); };
const oldInsert = session.insert; session.insert = function (position, text) { return oldInsert.apply(this, [position, outSideRange(position) ? text : ""]); };
const oldRemove = session.remove; session.remove = function (range) { return intersectsRange(range) ? false : oldRemove.apply(this, arguments); };
const oldMoveText = session.moveText; session.moveText = function (fromRange, toPosition, copy) { if (intersectsRange(fromRange) || !outSideRange(toPosition)) return fromRange; return oldMoveText.apply(this, arguments); }; }
export default setReadOnly;
|