feat: add editor.getAttributes, deprecate editor.getNodeAttributes and editor.getMarkAttributes

This commit is contained in:
Philipp Kühn 2021-05-07 11:10:18 +02:00
parent 58f49b563c
commit 072905cb95
5 changed files with 47 additions and 5 deletions

View File

@ -17,8 +17,7 @@ Dont 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<br>`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. |

View File

@ -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<string, any> {
return getAttributes(this.state, nameOrType)
}
/**
* Get attributes of the currently selected node.
*
* @param name Name of the node
*/
public getNodeAttributes(name: string): Record<string, any> {
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<string, any> {
console.warn('[tiptap warn]: editor.getMarkAttributes() is deprecated. please use editor.getAttributes() instead.')
return getMarkAttributes(this.state, name)
}

View File

@ -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<string, any> {
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 {}
}

View File

@ -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'

View File

@ -116,7 +116,7 @@ export const Link = Mark.create<LinkOptions>({
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) {