Files
outline/shared/editor/extensions/TrailingNode.ts
Tom MoorandClaude Opus 4.7 8c716b173a chore: Update editor generics (#12247)
* chore: Update editor generics

* fix: Address PR review on editor generics

- Restore null-guard on Link click handler so anchors aren't inert when no onClickLink is provided
- Mark onClickLink optional in LinkOptions and openLink command to match runtime
- Remove dead `collapsed` option from HeadingOptions
- Make ToggleBlock dictionary optional and restore optional-chained access for server-side schema instantiation

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-02 18:54:27 -04:00

89 lines
2.5 KiB
TypeScript

import type { NodeType } from "prosemirror-model";
import { Plugin, PluginKey } from "prosemirror-state";
import Extension from "../lib/Extension";
/**
* Options for the TrailingNode extension.
*/
type TrailingNodeOptions = {
/** Name of the node type to insert as the trailing node. */
node: string;
/** Node names after which a trailing node should not be inserted. */
notAfter: string[];
};
export default class TrailingNode extends Extension<TrailingNodeOptions> {
get name() {
return "trailing_node";
}
get defaultOptions(): TrailingNodeOptions {
return {
node: "paragraph",
notAfter: ["paragraph", "heading"],
};
}
get plugins() {
const plugin = new PluginKey(this.name);
const disabledNodes = Object.entries(this.editor.schema.nodes)
.map(([, value]) => value)
.filter((node: NodeType) => this.options.notAfter.includes(node.name));
return [
new Plugin({
key: plugin,
view: () => ({
update: (view) => {
const { state } = view;
const insertNodeAtEnd = plugin.getState(state);
if (!insertNodeAtEnd || !view.editable) {
return;
}
const { doc, schema, tr } = state;
const type = schema.nodes[this.options.node];
const transaction = tr.insert(doc.content.size, type.create());
view.dispatch(transaction);
},
}),
state: {
init: (_, state) => {
const lastNode = state.tr.doc.lastChild;
// If paragraph has no text (only images/media), add trailing node
if (
lastNode?.type.name === "paragraph" &&
lastNode.content.size > 0 &&
lastNode.textContent.length === 0
) {
return true;
}
return lastNode ? !disabledNodes.includes(lastNode.type) : false;
},
apply: (tr, value) => {
if (!tr.docChanged) {
return value;
}
const lastNode = tr.doc.lastChild;
// If paragraph has no text (only images/media), add trailing node
if (
lastNode?.type.name === "paragraph" &&
lastNode.content.size > 0 &&
lastNode.textContent.length === 0
) {
return true;
}
return lastNode ? !disabledNodes.includes(lastNode.type) : false;
},
},
}),
];
}
}