Files
plainleaf/web/cm_util.ts
T
MaksandGitHub 8a3782e946 improve dependency management (#770)
* use import_map.json instead of per-file import

* remove test_deps.ts in favor of import_map

* remove common/deps.ts in favor of import_map

* use import_map instead of server/deps.ts

* make all plug-api files usable without an import_map

* make all lib files usable without an import_map

* remove web/deps.ts in favor of import_map

* fix plug build

* remove plug-api/deps.ts in favor of lib/deps.ts

* fix server deps bundled in client deps
2024-03-16 15:29:24 +01:00

24 lines
705 B
TypeScript

import diff, { DELETE, INSERT } from "fast-diff";
import type { ChangeSpec } from "@codemirror/state";
export function diffAndPrepareChanges(
oldString: string,
newString: string,
): ChangeSpec[] {
// Use the fast-diff library to compute the changes
const diffs = diff(oldString, newString);
// Convert the diffs to CodeMirror transactions
let startIndex = 0;
const changes: ChangeSpec[] = [];
for (const part of diffs) {
if (part[0] === INSERT) {
changes.push({ from: startIndex, insert: part[1] });
} else if (part[0] === DELETE) {
changes.push({ from: startIndex, to: startIndex + part[1].length });
}
startIndex += part[1].length;
}
return changes;
}