Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editor: add insertNode util #6383

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions panel/src/components/Forms/Writer/Nodes/HardBreak.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ export default class HardBreak extends Node {
}

createHardBreak(utils, type) {
return utils.chainCommands(utils.exitCode, (state, dispatch) => {
dispatch(state.tr.replaceSelectionWith(type.create()).scrollIntoView());
return true;
});
return utils.chainCommands(utils.exitCode, utils.insertNode(type));
}

get defaults() {
Expand Down
5 changes: 2 additions & 3 deletions panel/src/components/Forms/Writer/Nodes/HorizontalRule.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import Node from "../Node";

export default class HorizontalRule extends Node {
commands({ type }) {
return () => (state, dispatch) =>
dispatch(state.tr.replaceSelectionWith(type.create()));
commands({ type, utils }) {
return () => utils.insertNode(type);
}

inputRules({ type, utils }) {
Expand Down
2 changes: 2 additions & 0 deletions panel/src/components/Forms/Writer/Utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
// custom
import getMarkAttrs from "./getMarkAttrs";
import getNodeAttrs from "./getNodeAttrs";
import insertNode from "./insertNode";
import markInputRule from "./markInputRule";
import markIsActive from "./markIsActive";
import markPasteRule from "./markPasteRule";
Expand Down Expand Up @@ -61,6 +62,7 @@ export default {
// custom
getMarkAttrs,
getNodeAttrs,
insertNode,
markInputRule,
markIsActive,
markPasteRule,
Expand Down
9 changes: 9 additions & 0 deletions panel/src/components/Forms/Writer/Utils/insertNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default function insertNode(type, attrs, content, marks) {
return (state, dispatch) => {
dispatch(
state.tr
.replaceSelectionWith(type.create(attrs, content, marks))
.scrollIntoView()
);
};
}
Loading