diff --git a/docs/src/demos/Examples/Basic/index.vue b/docs/src/demos/Examples/Basic/index.vue index 1f999876..1de6d03c 100644 --- a/docs/src/demos/Examples/Basic/index.vue +++ b/docs/src/demos/Examples/Basic/index.vue @@ -46,10 +46,10 @@ ordered list - + code block - + blockquote diff --git a/docs/src/demos/Examples/CollaborativeEditing/index.vue b/docs/src/demos/Examples/CollaborativeEditing/index.vue index be505434..3d762d80 100644 --- a/docs/src/demos/Examples/CollaborativeEditing/index.vue +++ b/docs/src/demos/Examples/CollaborativeEditing/index.vue @@ -46,10 +46,10 @@ ordered list - + code block - + blockquote diff --git a/docs/src/demos/Examples/CollaborativeEditingWs/index.vue b/docs/src/demos/Examples/CollaborativeEditingWs/index.vue index 9a2b63e7..08c3f452 100644 --- a/docs/src/demos/Examples/CollaborativeEditingWs/index.vue +++ b/docs/src/demos/Examples/CollaborativeEditingWs/index.vue @@ -46,10 +46,10 @@ ordered list - + code block - + blockquote diff --git a/docs/src/demos/Extensions/FontFamily/index.vue b/docs/src/demos/Extensions/FontFamily/index.vue index b2a43a4a..7ecd28e2 100644 --- a/docs/src/demos/Extensions/FontFamily/index.vue +++ b/docs/src/demos/Extensions/FontFamily/index.vue @@ -1,21 +1,21 @@ - + Inter - + Comic Sans - + serif - + monospace - + cursive - + Remove font-family 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/docs/src/demos/Nodes/CodeBlock/index.vue b/docs/src/demos/Nodes/CodeBlock/index.vue index 4da9c34e..d80b9aa3 100644 --- a/docs/src/demos/Nodes/CodeBlock/index.vue +++ b/docs/src/demos/Nodes/CodeBlock/index.vue @@ -1,6 +1,6 @@ - + code block 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(), } }, diff --git a/packages/extension-code-block/src/index.ts b/packages/extension-code-block/src/index.ts index 2d697e9d..d6bcc1b9 100644 --- a/packages/extension-code-block/src/index.ts +++ b/packages/extension-code-block/src/index.ts @@ -74,18 +74,24 @@ const CodeBlock = Node.create({ addCommands() { return { + /** + * Set a code block + */ + setCodeBlock: (attributes?: { language: string }): Command => ({ commands }) => { + return commands.setBlockType('codeBlock', attributes) + }, /** * Toggle a code block */ - codeBlock: (attrs?: CodeBlockOptions): Command => ({ commands }) => { - return commands.toggleBlockType('codeBlock', 'paragraph', attrs) + toggleCodeBlock: (attributes?: { language: string }): Command => ({ commands }) => { + return commands.toggleBlockType('codeBlock', 'paragraph', attributes) }, } }, addKeyboardShortcuts() { return { - 'Mod-Shift-c': () => this.editor.commands.codeBlock(), + 'Mod-Shift-c': () => this.editor.commands.toggleCodeBlock(), } }, diff --git a/packages/extension-font-family/src/index.ts b/packages/extension-font-family/src/index.ts index c1eebf91..c29cad73 100644 --- a/packages/extension-font-family/src/index.ts +++ b/packages/extension-font-family/src/index.ts @@ -38,11 +38,19 @@ const FontFamily = Extension.create({ addCommands() { return { /** - * Update the font family + * Set the font family */ - fontFamily: (fontFamily: string | null = null): Command => ({ chain }) => { + setFontFamily: (fontFamily: string): Command => ({ chain }) => { return chain() .addMark('textStyle', { fontFamily }) + .run() + }, + /** + * Unset the font family + */ + unsetFontFamily: (): Command => ({ chain }) => { + return chain() + .addMark('textStyle', { fontFamily: null }) .removeEmptyTextStyle() .run() },