diff --git a/docs/src/demos/Examples/Basic/index.vue b/docs/src/demos/Examples/Basic/index.vue index 1f999876..e37494a9 100644 --- a/docs/src/demos/Examples/Basic/index.vue +++ b/docs/src/demos/Examples/Basic/index.vue @@ -49,7 +49,7 @@ code block - + blockquote diff --git a/docs/src/demos/Examples/CollaborativeEditing/index.vue b/docs/src/demos/Examples/CollaborativeEditing/index.vue index be505434..eb03cf0a 100644 --- a/docs/src/demos/Examples/CollaborativeEditing/index.vue +++ b/docs/src/demos/Examples/CollaborativeEditing/index.vue @@ -49,7 +49,7 @@ code block - + blockquote diff --git a/docs/src/demos/Examples/CollaborativeEditingWs/index.vue b/docs/src/demos/Examples/CollaborativeEditingWs/index.vue index 9a2b63e7..e186a45f 100644 --- a/docs/src/demos/Examples/CollaborativeEditingWs/index.vue +++ b/docs/src/demos/Examples/CollaborativeEditingWs/index.vue @@ -49,7 +49,7 @@ code block - + blockquote diff --git a/docs/src/demos/Nodes/Blockquote/index.vue b/docs/src/demos/Nodes/Blockquote/index.vue index 15148420..e8c6d2b1 100644 --- a/docs/src/demos/Nodes/Blockquote/index.vue +++ b/docs/src/demos/Nodes/Blockquote/index.vue @@ -1,6 +1,6 @@ - + blockquote diff --git a/packages/core/src/commands/lift.ts b/packages/core/src/commands/lift.ts new file mode 100644 index 00000000..f9594247 --- /dev/null +++ b/packages/core/src/commands/lift.ts @@ -0,0 +1,16 @@ +import { lift } from 'prosemirror-commands' +import { NodeType } from 'prosemirror-model' +import { Command } from '../types' +import nodeIsActive from '../utils/nodeIsActive' +import getNodeType from '../utils/getNodeType' + +export default (typeOrName: string | NodeType, attributes = {}): Command => ({ state, dispatch }) => { + const type = getNodeType(typeOrName, state.schema) + const isActive = nodeIsActive(state, type, attributes) + + if (!isActive) { + return false + } + + return lift(state, dispatch) +} diff --git a/packages/core/src/commands/toggleWrap.ts b/packages/core/src/commands/toggleWrap.ts index c72dd0a5..72b45848 100644 --- a/packages/core/src/commands/toggleWrap.ts +++ b/packages/core/src/commands/toggleWrap.ts @@ -4,13 +4,13 @@ import { Command } from '../types' import nodeIsActive from '../utils/nodeIsActive' import getNodeType from '../utils/getNodeType' -export default (typeOrName: string | NodeType, attrs = {}): Command => ({ state, dispatch }) => { +export default (typeOrName: string | NodeType, attributes = {}): Command => ({ state, dispatch }) => { const type = getNodeType(typeOrName, state.schema) - const isActive = nodeIsActive(state, type, attrs) + const isActive = nodeIsActive(state, type, attributes) if (isActive) { return lift(state, dispatch) } - return wrapIn(type, attrs)(state, dispatch) + return wrapIn(type, attributes)(state, dispatch) } diff --git a/packages/core/src/commands/wrapIn.ts b/packages/core/src/commands/wrapIn.ts new file mode 100644 index 00000000..9d1b2d75 --- /dev/null +++ b/packages/core/src/commands/wrapIn.ts @@ -0,0 +1,16 @@ +import { wrapIn } from 'prosemirror-commands' +import { NodeType } from 'prosemirror-model' +import { Command } from '../types' +import nodeIsActive from '../utils/nodeIsActive' +import getNodeType from '../utils/getNodeType' + +export default (typeOrName: string | NodeType, attributes = {}): Command => ({ state, dispatch }) => { + const type = getNodeType(typeOrName, state.schema) + const isActive = nodeIsActive(state, type, attributes) + + if (isActive) { + return false + } + + return wrapIn(type, attributes)(state, dispatch) +} diff --git a/packages/core/src/extensions/commands.ts b/packages/core/src/extensions/commands.ts index f10b2701..9d647dc2 100644 --- a/packages/core/src/extensions/commands.ts +++ b/packages/core/src/extensions/commands.ts @@ -9,6 +9,7 @@ import extendMarkRange from '../commands/extendMarkRange' import focus from '../commands/focus' import insertHTML from '../commands/insertHTML' import insertText from '../commands/insertText' +import lift from '../commands/lift' import liftListItem from '../commands/liftListItem' import removeMark from '../commands/removeMark' import removeMarks from '../commands/removeMarks' @@ -27,6 +28,7 @@ import toggleMark from '../commands/toggleMark' import toggleWrap from '../commands/toggleWrap' import tryCommand from '../commands/try' import updateNodeAttributes from '../commands/updateNodeAttributes' +import wrapIn from '../commands/wrapIn' import wrapInList from '../commands/wrapInList' export const Commands = Extension.create({ @@ -72,6 +74,10 @@ export const Commands = Extension.create({ * Insert a string of text at the current position. */ insertText, + /** + * Removes an existing wrap. + */ + lift, /** * Lift the list item into a wrapping list. */ @@ -144,6 +150,10 @@ export const Commands = Extension.create({ * Update attributes of a node. */ updateNodeAttributes, + /** + * Wraps nodes in another node. + */ + wrapIn, /** * Wrap a node in a list. */ diff --git a/packages/extension-blockquote/src/index.ts b/packages/extension-blockquote/src/index.ts index d0d3f318..60ae7c0e 100644 --- a/packages/extension-blockquote/src/index.ts +++ b/packages/extension-blockquote/src/index.ts @@ -34,18 +34,30 @@ const Blockquote = Node.create({ addCommands() { return { + /** + * Set a blockquote node + */ + setBlockquote: (): Command => ({ commands }) => { + return commands.wrapIn('blockquote') + }, /** * Toggle a blockquote node */ - blockquote: (): Command => ({ commands }) => { + toggleBlockquote: (): Command => ({ commands }) => { return commands.toggleWrap('blockquote') }, + /** + * Unset a blockquote node + */ + unsetBlockquote: (): Command => ({ commands }) => { + return commands.lift('blockquote') + }, } }, addKeyboardShortcuts() { return { - 'Shift-Mod-9': () => this.editor.commands.blockquote(), + 'Shift-Mod-9': () => this.editor.commands.toggleBlockquote(), } },