diff --git a/docs/src/docPages/api/editor.md b/docs/src/docPages/api/editor.md index 4e237f31..08c2a0c6 100644 --- a/docs/src/docPages/api/editor.md +++ b/docs/src/docPages/api/editor.md @@ -17,8 +17,7 @@ Don’t confuse methods with [commands](/api/commands). Commands are used to cha | `destroy()` | – | Stops the editor instance and unbinds all events. | | `getHTML()` | – | Returns the current content as HTML. | | `getJSON()` | – | Returns the current content as JSON. | -| `getMarkAttributes()` | `name` Name of the mark | Get attributes of the currently selected mark. | -| `getNodeAttributes()` | `name` Name of the node | Get attributes of the currently selected node. | +| `getAttributes()` | `name` Name of the node or mark | Get attributes of the currently selected node or mark. | | `isActive()` | `name` Name of the node or mark
`attrs` Attributes of the node or mark | Returns if the currently selected node or mark is active. | | `isEditable()` | - | Returns whether the editor is editable. | | `isEmpty()` | - | Check if there is no content. | diff --git a/packages/core/src/Editor.ts b/packages/core/src/Editor.ts index fbc75e48..988ec3ec 100644 --- a/packages/core/src/Editor.ts +++ b/packages/core/src/Editor.ts @@ -1,8 +1,12 @@ import { - EditorState, Plugin, PluginKey, Transaction, + EditorState, + Plugin, + PluginKey, + Transaction, } from 'prosemirror-state' import { EditorView } from 'prosemirror-view' -import { Schema } from 'prosemirror-model' +import { Schema, MarkType, NodeType } from 'prosemirror-model' +import getAttributes from './helpers/getAttributes' import getNodeAttributes from './helpers/getNodeAttributes' import getMarkAttributes from './helpers/getMarkAttributes' import isActive from './helpers/isActive' @@ -331,12 +335,21 @@ export class Editor extends EventEmitter { }) } + /** + * Get attributes of the currently selected node or mark. + */ + public getAttributes(nameOrType: string | NodeType | MarkType): Record { + return getAttributes(this.state, nameOrType) + } + /** * Get attributes of the currently selected node. * * @param name Name of the node */ public getNodeAttributes(name: string): Record { + console.warn('[tiptap warn]: editor.getNodeAttributes() is deprecated. please use editor.getAttributes() instead.') + return getNodeAttributes(this.state, name) } @@ -346,6 +359,8 @@ export class Editor extends EventEmitter { * @param name Name of the mark */ public getMarkAttributes(name: string): Record { + console.warn('[tiptap warn]: editor.getMarkAttributes() is deprecated. please use editor.getAttributes() instead.') + return getMarkAttributes(this.state, name) } diff --git a/packages/core/src/helpers/getAttributes.ts b/packages/core/src/helpers/getAttributes.ts new file mode 100644 index 00000000..6c06fbe4 --- /dev/null +++ b/packages/core/src/helpers/getAttributes.ts @@ -0,0 +1,27 @@ +import { MarkType, NodeType } from 'prosemirror-model' +import { EditorState } from 'prosemirror-state' +import getSchemaTypeNameByName from './getSchemaTypeNameByName' +import getNodeAttributes from './getNodeAttributes' +import getMarkAttributes from './getMarkAttributes' + +export default function getAttributes( + state: EditorState, + typeOrName: string | NodeType | MarkType, +): Record { + const schemaType = getSchemaTypeNameByName( + typeof typeOrName === 'string' + ? typeOrName + : typeOrName.name, + state.schema, + ) + + if (schemaType === 'node') { + return getNodeAttributes(state, typeOrName as NodeType) + } + + if (schemaType === 'mark') { + return getMarkAttributes(state, typeOrName as MarkType) + } + + return {} +} diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 0bec85ec..31f650c0 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -21,6 +21,7 @@ export { default as generateHTML } from './helpers/generateHTML' export { default as generateJSON } from './helpers/generateJSON' export { default as getSchema } from './helpers/getSchema' export { default as getHTMLFromFragment } from './helpers/getHTMLFromFragment' +export { default as getAttributes } from './helpers/getMarkAttributes' export { default as getMarkAttributes } from './helpers/getMarkAttributes' export { default as getNodeAttributes } from './helpers/getNodeAttributes' export { default as getNodeType } from './helpers/getNodeType' diff --git a/packages/extension-link/src/link.ts b/packages/extension-link/src/link.ts index 7db16184..36e5ce2c 100644 --- a/packages/extension-link/src/link.ts +++ b/packages/extension-link/src/link.ts @@ -116,7 +116,7 @@ export const Link = Mark.create({ key: new PluginKey('handleClickLink'), props: { handleClick: (view, pos, event) => { - const attrs = this.editor.getMarkAttributes('link') + const attrs = this.editor.getAttributes('link') const link = (event.target as HTMLElement)?.closest('a') if (link && attrs.href) {