mirror of
https://github.com/outline/outline.git
synced 2026-11-21 05:24:03 +03:00
Avoid full document traversal on every keystroke by mapping decorations through the transaction when no relevant nodes changed. Uses changedDescendants to detect when a heading, image, or code_inline-marked text actually changes; otherwise the existing DecorationSet is mapped to new positions cheaply. Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
93 lines
2.7 KiB
TypeScript
93 lines
2.7 KiB
TypeScript
import type { NodeAnchor } from "@shared/utils/ProsemirrorHelper";
|
|
import { ProsemirrorHelper } from "@shared/utils/ProsemirrorHelper";
|
|
import type { Node } from "prosemirror-model";
|
|
import type { EditorState, Transaction } from "prosemirror-state";
|
|
import { Plugin } from "prosemirror-state";
|
|
import { Decoration, DecorationSet } from "prosemirror-view";
|
|
import { changedDescendants } from "../lib/changedDescendants";
|
|
import { isRemoteTransaction } from "../lib/multiplayer";
|
|
|
|
export class AnchorPlugin extends Plugin {
|
|
constructor() {
|
|
super({
|
|
state: {
|
|
init: (_, state: EditorState) => ({
|
|
decorations: this.createDecorations(state),
|
|
}),
|
|
apply: (tr, pluginState, _oldState, newState) => {
|
|
if (!tr.docChanged) {
|
|
return pluginState;
|
|
}
|
|
|
|
if (isRemoteTransaction(tr) || this.hasAnchorableChange(tr)) {
|
|
return { decorations: this.createDecorations(newState) };
|
|
}
|
|
|
|
return {
|
|
decorations: pluginState.decorations.map(tr.mapping, tr.doc),
|
|
};
|
|
},
|
|
},
|
|
props: {
|
|
decorations: (state) => {
|
|
const pluginState = this.getState(state);
|
|
return pluginState ? pluginState.decorations : null;
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
private isAnchorable(node: Node): boolean {
|
|
return (
|
|
node.type.name === "heading" ||
|
|
(Array.isArray(node.attrs.marks) &&
|
|
node.attrs.marks.some(
|
|
(mark: { type: string; attrs?: { id?: string } }) =>
|
|
mark.type === "comment" && mark.attrs?.id
|
|
))
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Check if the transaction changed any heading or image-with-comment-mark
|
|
* nodes by comparing changed descendants in both directions.
|
|
*/
|
|
private hasAnchorableChange(tr: Transaction): boolean {
|
|
let found = false;
|
|
const check = (node: Node) => {
|
|
if (!found && this.isAnchorable(node)) {
|
|
found = true;
|
|
}
|
|
};
|
|
|
|
changedDescendants(tr.before, tr.doc, 0, check);
|
|
if (!found) {
|
|
changedDescendants(tr.doc, tr.before, 0, check);
|
|
}
|
|
return found;
|
|
}
|
|
|
|
private createAnchorDecoration(anchor: NodeAnchor) {
|
|
return Decoration.widget(
|
|
anchor.pos,
|
|
() => {
|
|
const anchorElement = document.createElement("a");
|
|
anchorElement.id = anchor.id;
|
|
anchorElement.className = anchor.className;
|
|
return anchorElement;
|
|
},
|
|
{ side: -1, key: anchor.id }
|
|
);
|
|
}
|
|
|
|
private createDecorations(state: EditorState) {
|
|
const anchors = ProsemirrorHelper.getAnchors(state.doc);
|
|
return DecorationSet.create(
|
|
state.doc,
|
|
anchors.map(this.createAnchorDecoration)
|
|
);
|
|
}
|
|
}
|
|
|
|
export const anchorPlugin = () => new AnchorPlugin();
|