codemirror-ot
Version:
Operational Transformation adapter for CodeMirror 6.
42 lines (40 loc) • 1.55 kB
JavaScript
// Returns true if an op can affect the given path.
// Inspired by
// https://github.com/ottypes/json0/blob/master/lib/json0.js#L410
//
// The purpose of this is to check whether or not a given remote op
// affects the path associated to the json1Sync instance.
// When multiple files are open and being edited, the CodeMirror
// instance for one file should ignore ops for all other files.
//
// Previous implementation in VizHub v2 using CodeMirror 5 and JSON0:
// https://github.com/vizhub-core/vizhub/blob/76d4ca43a8b0f3c543919ccc66a7228d75ba37cd/vizhub-v2/packages/neoFrontend/src/pages/VizPage/Body/Editor/CodeEditor/CodeArea/CodeAreaCodeMirror5/index.js#L144
const debug = false;
export const canOpAffectPath = (op, path) => {
// Defense
if (!op || !path) {
if (debug) {
console.log(' canOpAffectPath: op or path is null');
}
return false;
}
// Current approach: check that the path matches the json1 "descent".
// Unclear if this is 100% correct for all possible ops,
// but it works with the ops generated by this library at least.
if (debug) {
console.log(' canOpAffectPath: comparing op and path');
console.log(' op: ' + JSON.stringify(op));
console.log(' path: ' + JSON.stringify(path));
}
for (let i = 0; i < path.length; i++) {
if (path[i] !== op[i]) {
if (debug) {
console.log(' mismatch at index ' + i);
console.log(' path[i]: ' + path[i]);
console.log(' op[i]: ' + op[i]);
}
return false;
}
}
return true;
};