Files
outline/shared/editor/plugins/AnchorPlugin.ts
T
Tom Moor f9a2cbc1b3 chore: resolve remaining unbound-method lint warnings (#12206)
* chore: resolve remaining unbound-method lint warnings

Apply targeted fixes per call pattern: arrow wrappers when passing a
method as a callback, arrow-function class fields when the method
doesn't depend on `this`, and `.bind()` when capturing for later
invocation.

Also replaces the rfc6902 hasOwnProperty re-export with a small wrapper
function so callers don't reference an unbound prototype method.

* chore: memoize history.goBack callbacks

Stable identity prevents Button re-renders and avoids re-subscribing
the global keydown handler in RegisterKeyDown when the parent renders.
2026-04-28 20:49:35 -04:00

53 lines
1.5 KiB
TypeScript

import type { NodeAnchor } from "@shared/utils/ProsemirrorHelper";
import { ProsemirrorHelper } from "@shared/utils/ProsemirrorHelper";
import type { EditorState } from "prosemirror-state";
import { Plugin } from "prosemirror-state";
import { Decoration, DecorationSet } from "prosemirror-view";
export class AnchorPlugin extends Plugin {
constructor() {
super({
state: {
init: (_, state: EditorState) => ({
decorations: this.createDecorations(state),
}),
apply: (tr, pluginState, oldState, newState) => {
// Only recompute if doc changed
if (tr.docChanged) {
return { decorations: this.createDecorations(newState) };
}
return pluginState;
},
},
props: {
decorations: (state) => {
const pluginState = this.getState(state);
return pluginState ? pluginState.decorations : null;
},
},
});
}
private createAnchorDecoration = (anchor: NodeAnchor) =>
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();