diff --git a/docs/src/demos/Extensions/TextAlign/index.vue b/docs/src/demos/Extensions/TextAlign/index.vue index 25ddd3bf..08ec6d50 100644 --- a/docs/src/demos/Extensions/TextAlign/index.vue +++ b/docs/src/demos/Extensions/TextAlign/index.vue @@ -9,7 +9,7 @@ - diff --git a/packages/core/src/extensions/clearNodes.ts b/packages/core/src/extensions/clearNodes.ts index 5d755767..cb4ab185 100644 --- a/packages/core/src/extensions/clearNodes.ts +++ b/packages/core/src/extensions/clearNodes.ts @@ -5,7 +5,7 @@ import { createExtension } from '../Extension' export const ClearNodes = createExtension({ addCommands() { return { - clearNodes: (): Command => ({ state, tr }) => { + clearNodes: (): Command => ({ state, tr, dispatch }) => { const { selection } = tr const { from, to } = selection @@ -18,11 +18,11 @@ export const ClearNodes = createExtension({ if (nodeRange) { const targetLiftDepth = liftTarget(nodeRange) - if (node.type.isTextblock) { + if (node.type.isTextblock && dispatch) { tr.setNodeMarkup(nodeRange.start, state.schema.nodes.paragraph) } - if (targetLiftDepth || targetLiftDepth === 0) { + if ((targetLiftDepth || targetLiftDepth === 0) && dispatch) { tr.lift(nodeRange, targetLiftDepth) } } diff --git a/packages/core/src/extensions/focus.ts b/packages/core/src/extensions/focus.ts index 8f9ffdb3..1719a6b8 100644 --- a/packages/core/src/extensions/focus.ts +++ b/packages/core/src/extensions/focus.ts @@ -40,7 +40,9 @@ function resolveSelection(editor: Editor, position: Position = null): ResolvedSe export const Focus = createExtension({ addCommands() { return { - focus: (position: Position = null): Command => ({ editor, view, tr }) => { + focus: (position: Position = null): Command => ({ + editor, view, tr, dispatch, + }) => { if ((view.hasFocus() && position === null) || position === false) { return true } @@ -51,7 +53,10 @@ export const Focus = createExtension({ const resolvedEnd = minMax(to, 0, doc.content.size) const selection = TextSelection.create(doc, resolvedFrom, resolvedEnd) - tr.setSelection(selection) + if (dispatch) { + tr.setSelection(selection) + } + view.focus() return true diff --git a/packages/core/src/extensions/index.ts b/packages/core/src/extensions/index.ts index f21bbac3..0d2de36f 100644 --- a/packages/core/src/extensions/index.ts +++ b/packages/core/src/extensions/index.ts @@ -11,7 +11,7 @@ export { RemoveMarks } from './removeMarks' export { ScrollIntoView } from './scrollIntoView' export { SelectAll } from './selectAll' export { SelectParentNode } from './selectParentNode' -export { SetDefaultNodeAttributes } from './setDefaultNodeAttributes' +export { ResetNodeAttributes } from './resetNodeAttributes' export { SetNodeAttributes } from './setNodeAttributes' export { SetBlockType } from './setBlockType' export { SetContent } from './setContent' diff --git a/packages/core/src/extensions/insertHTML.ts b/packages/core/src/extensions/insertHTML.ts index 2a567a40..8d0f395f 100644 --- a/packages/core/src/extensions/insertHTML.ts +++ b/packages/core/src/extensions/insertHTML.ts @@ -21,13 +21,15 @@ function selectionToInsertionEnd(tr: Transaction, startLen: number, bias: number export const InsertHTML = createExtension({ addCommands() { return { - insertHTML: (value: string): Command => ({ tr, state }) => { + insertHTML: (value: string): Command => ({ tr, state, dispatch }) => { const { selection } = tr const element = elementFromString(value) const slice = DOMParser.fromSchema(state.schema).parseSlice(element) - tr.insert(selection.anchor, slice.content) - selectionToInsertionEnd(tr, tr.steps.length - 1, -1) + if (dispatch) { + tr.insert(selection.anchor, slice.content) + selectionToInsertionEnd(tr, tr.steps.length - 1, -1) + } return true }, diff --git a/packages/core/src/extensions/insertText.ts b/packages/core/src/extensions/insertText.ts index cdef3263..a7302fe0 100644 --- a/packages/core/src/extensions/insertText.ts +++ b/packages/core/src/extensions/insertText.ts @@ -4,8 +4,10 @@ import { createExtension } from '../Extension' export const InsertText = createExtension({ addCommands() { return { - insertText: (value: string): Command => ({ tr }) => { - tr.insertText(value) + insertText: (value: string): Command => ({ tr, dispatch }) => { + if (dispatch) { + tr.insertText(value) + } return true }, diff --git a/packages/core/src/extensions/removeMark.ts b/packages/core/src/extensions/removeMark.ts index f4463341..3bd0ccf3 100644 --- a/packages/core/src/extensions/removeMark.ts +++ b/packages/core/src/extensions/removeMark.ts @@ -7,7 +7,7 @@ import getMarkRange from '../utils/getMarkRange' export const RemoveMark = createExtension({ addCommands() { return { - removeMark: (typeOrName: string | MarkType): Command => ({ tr, state }) => { + removeMark: (typeOrName: string | MarkType): Command => ({ tr, state, dispatch }) => { const { selection } = tr const type = getMarkType(typeOrName, state.schema) let { from, to } = selection @@ -22,7 +22,9 @@ export const RemoveMark = createExtension({ } } - tr.removeMark(from, to, type) + if (dispatch) { + tr.removeMark(from, to, type) + } return true }, diff --git a/packages/core/src/extensions/removeMarks.ts b/packages/core/src/extensions/removeMarks.ts index af527062..fcc5ed1b 100644 --- a/packages/core/src/extensions/removeMarks.ts +++ b/packages/core/src/extensions/removeMarks.ts @@ -4,7 +4,7 @@ import { createExtension } from '../Extension' export const RemoveMarks = createExtension({ addCommands() { return { - removeMarks: (): Command => ({ tr, state }) => { + removeMarks: (): Command => ({ tr, state, dispatch }) => { const { selection } = tr const { from, to, empty } = selection @@ -12,11 +12,13 @@ export const RemoveMarks = createExtension({ return true } - Object - .entries(state.schema.marks) - .forEach(([, mark]) => { - tr.removeMark(from, to, mark as any) - }) + if (dispatch) { + Object + .entries(state.schema.marks) + .forEach(([, mark]) => { + tr.removeMark(from, to, mark as any) + }) + } return true }, diff --git a/packages/core/src/extensions/setDefaultNodeAttributes.ts b/packages/core/src/extensions/resetNodeAttributes.ts similarity index 71% rename from packages/core/src/extensions/setDefaultNodeAttributes.ts rename to packages/core/src/extensions/resetNodeAttributes.ts index 8347b141..a9babeff 100644 --- a/packages/core/src/extensions/setDefaultNodeAttributes.ts +++ b/packages/core/src/extensions/resetNodeAttributes.ts @@ -1,10 +1,10 @@ import { Command } from '../Editor' import { createExtension } from '../Extension' -export const SetDefaultNodeAttributes = createExtension({ +export const ResetNodeAttributes = createExtension({ addCommands() { return { - setDefaultNodeAttributes: (attributeNames: string[] = []): Command => ({ tr, state }) => { + resetNodeAttributes: (attributeNames: string[] = []): Command => ({ tr, state, dispatch }) => { const { selection } = tr const { from, to } = selection @@ -14,7 +14,7 @@ export const SetDefaultNodeAttributes = createExtension({ const attribute = node.type.spec.attrs?.[name] const defaultValue = attribute?.default - if (attribute && defaultValue !== undefined) { + if (attribute && defaultValue !== undefined && dispatch) { tr.setNodeMarkup(pos, undefined, { [name]: defaultValue, }) @@ -31,6 +31,6 @@ export const SetDefaultNodeAttributes = createExtension({ declare module '../Editor' { interface AllExtensions { - SetDefaultNodeAttributes: typeof SetDefaultNodeAttributes, + ResetNodeAttributes: typeof ResetNodeAttributes, } } diff --git a/packages/core/src/extensions/scrollIntoView.ts b/packages/core/src/extensions/scrollIntoView.ts index 44fee958..30dc3d9e 100644 --- a/packages/core/src/extensions/scrollIntoView.ts +++ b/packages/core/src/extensions/scrollIntoView.ts @@ -4,8 +4,10 @@ import { createExtension } from '../Extension' export const ScrollIntoView = createExtension({ addCommands() { return { - scrollIntoView: (): Command => ({ tr }) => { - tr.scrollIntoView() + scrollIntoView: (): Command => ({ tr, dispatch }) => { + if (dispatch) { + tr.scrollIntoView() + } return true }, diff --git a/packages/core/src/extensions/setContent.ts b/packages/core/src/extensions/setContent.ts index f19fb906..a9069f1e 100644 --- a/packages/core/src/extensions/setContent.ts +++ b/packages/core/src/extensions/setContent.ts @@ -5,15 +5,17 @@ import { createExtension } from '../Extension' export const SetContent = createExtension({ addCommands() { return { - setContent: (content: string, emitUpdate: Boolean = false, parseOptions = {}): Command => ({ tr, editor }) => { + setContent: (content: string, emitUpdate: Boolean = false, parseOptions = {}): Command => ({ tr, editor, dispatch }) => { const { createDocument } = editor const { doc } = tr const document = createDocument(content, parseOptions) const selection = TextSelection.create(doc, 0, doc.content.size) - tr.setSelection(selection) - .replaceSelectionWith(document, false) - .setMeta('preventUpdate', !emitUpdate) + if (dispatch) { + tr.setSelection(selection) + .replaceSelectionWith(document, false) + .setMeta('preventUpdate', !emitUpdate) + } return true }, diff --git a/packages/core/src/extensions/setNodeAttributes.ts b/packages/core/src/extensions/setNodeAttributes.ts index d049ba3b..9333a554 100644 --- a/packages/core/src/extensions/setNodeAttributes.ts +++ b/packages/core/src/extensions/setNodeAttributes.ts @@ -4,12 +4,12 @@ import { createExtension } from '../Extension' export const SetNodeAttributes = createExtension({ addCommands() { return { - setNodeAttributes: (attributes: {}): Command => ({ tr, state }) => { + setNodeAttributes: (attributes: {}): Command => ({ tr, state, dispatch }) => { const { selection } = tr const { from, to } = selection state.doc.nodesBetween(from, to, (node, pos) => { - if (!node.type.isText) { + if (!node.type.isText && dispatch) { tr.setNodeMarkup(pos, undefined, attributes) } }) diff --git a/packages/core/src/extensions/splitBlock.ts b/packages/core/src/extensions/splitBlock.ts index 37aaa7da..39b78241 100644 --- a/packages/core/src/extensions/splitBlock.ts +++ b/packages/core/src/extensions/splitBlock.ts @@ -77,7 +77,7 @@ export const SplitBlock = createExtension({ } } - dispatch(tr.scrollIntoView()) + tr.scrollIntoView() } return true diff --git a/packages/core/src/extensions/toggleList.ts b/packages/core/src/extensions/toggleList.ts index acfaf05b..fe8bcc2b 100644 --- a/packages/core/src/extensions/toggleList.ts +++ b/packages/core/src/extensions/toggleList.ts @@ -31,10 +31,12 @@ export const ToggleList = createExtension({ } // change list type - if (isList(parentList.node.type.name, extensions) && listType.validContent(parentList.node.content)) { - if (dispatch) { - tr.setNodeMarkup(parentList.pos, listType) - } + if ( + isList(parentList.node.type.name, extensions) + && listType.validContent(parentList.node.content) + && dispatch + ) { + tr.setNodeMarkup(parentList.pos, listType) return true } diff --git a/packages/core/src/extensions/updateMark.ts b/packages/core/src/extensions/updateMark.ts index 0200cf0d..5c7885c0 100644 --- a/packages/core/src/extensions/updateMark.ts +++ b/packages/core/src/extensions/updateMark.ts @@ -7,7 +7,7 @@ import getMarkRange from '../utils/getMarkRange' export const UpdateMark = createExtension({ addCommands() { return { - updateMark: (typeOrName: string | MarkType, attrs: {}): Command => ({ tr, state }) => { + updateMark: (typeOrName: string | MarkType, attrs: {}): Command => ({ tr, state, dispatch }) => { const { selection, doc } = tr let { from, to } = selection const { $from, empty } = selection @@ -24,11 +24,13 @@ export const UpdateMark = createExtension({ const hasMark = doc.rangeHasMark(from, to, type) - if (hasMark) { + if (hasMark && dispatch) { tr.removeMark(from, to, type) } - tr.addMark(from, to, type.create(attrs)) + if (dispatch) { + tr.addMark(from, to, type.create(attrs)) + } return true },