Files
outline/shared/editor/plugins/PlaceholderPlugin.ts
T
Tom Moor 0139b91b5d chore: Replace lodash with es-toolkit (#12281)
* chore: Replace lodash with es-toolkit

Migrate all direct lodash imports to es-toolkit/compat for a smaller,
faster, lodash-compatible utility library. Transitive lodash usage from
other packages remains unchanged.

* fix: Restore isPlainObject semantics in CanCan policy

The lodash migration aliased `isObject` to `lodash/isPlainObject` and
the codemod incorrectly mapped the local name to es-toolkit's `isObject`,
which also returns true for arrays and functions. This caused condition
objects in policy definitions to be skipped, breaking authorization
checks across the codebase.

* fix: Restore unicode-aware length counting in validators

es-toolkit/compat's size() returns string.length, while lodash's _.size()
counts unicode code points. Switch to [...value].length to preserve the
previous behavior so multi-byte characters like emoji count as one.
2026-05-06 21:03:47 -04:00

91 lines
2.7 KiB
TypeScript

import { filter, find, map } from "es-toolkit/compat";
import type { Node, ResolvedPos } from "prosemirror-model";
import type { EditorState } from "prosemirror-state";
import { Plugin } from "prosemirror-state";
import { Decoration, DecorationSet } from "prosemirror-view";
type Config = Array<{
/** Condition to meet for the placeholder to be applied to a node */
condition: (args: {
/** Node to which the placeholder is expected to be applied */
node: Node;
/** Resolved position corresponding to start of node */
$start: ResolvedPos;
/** Parent of node to which the placeholder is expected to be applied */
parent: Node | null;
/** Current editor state */
state: EditorState;
/** Text content of the document */
textContent: string;
}) => boolean;
/** Placeholder text */
text: string;
}>;
export class PlaceholderPlugin extends Plugin {
constructor(config: Config) {
super({
state: {
init: (_, state: EditorState) => ({
decorations: this.createDecorations(state, config),
}),
apply: (tr, pluginState, oldState, newState) => {
// Only recompute if doc or selection changed
if (tr.docChanged || tr.selectionSet) {
return { decorations: this.createDecorations(newState, config) };
}
return pluginState;
},
},
props: {
decorations: (state) => {
const pluginState = this.getState(state);
return pluginState ? pluginState.decorations : null;
},
},
});
}
private createDecorations(state: EditorState, config: Config) {
const paras: Array<{
node: Node;
$start: ResolvedPos;
parent: Node | null;
}> = [];
state.doc.descendants((node, pos, parent) => {
if (node.type.name === "paragraph") {
paras.push({ node, $start: state.doc.resolve(pos + 1), parent });
return false;
}
return true;
});
const textContent = state.doc.textContent;
const decorations: Decoration[] = filter(
map(paras, (para) => {
const condMet = find(config, (conf) =>
conf.condition({
node: para.node,
$start: para.$start,
parent: para.parent,
state,
textContent,
})
);
return condMet
? Decoration.node(
para.$start.pos - 1,
para.$start.pos - 1 + para.node.nodeSize,
{
class: "placeholder",
"data-empty-text": condMet.text,
}
)
: undefined;
}),
(decoration) => decoration !== undefined
);
return DecorationSet.create(state.doc, decorations);
}
}