feat: add editor.getAttributes, deprecate editor.getNodeAttributes and editor.getMarkAttributes
This commit is contained in:
@@ -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. |
|
| `destroy()` | – | Stops the editor instance and unbinds all events. |
|
||||||
| `getHTML()` | – | Returns the current content as HTML. |
|
| `getHTML()` | – | Returns the current content as HTML. |
|
||||||
| `getJSON()` | – | Returns the current content as JSON. |
|
| `getJSON()` | – | Returns the current content as JSON. |
|
||||||
| `getMarkAttributes()` | `name` Name of the mark | Get attributes of the currently selected mark. |
|
| `getAttributes()` | `name` Name of the node or mark | Get attributes of the currently selected node or mark. |
|
||||||
| `getNodeAttributes()` | `name` Name of the node | Get attributes of the currently selected node. |
|
|
||||||
| `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. |
|
| `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. |
|
| `isEditable()` | - | Returns whether the editor is editable. |
|
||||||
| `isEmpty()` | - | Check if there is no content. |
|
| `isEmpty()` | - | Check if there is no content. |
|
||||||
|
|||||||
@@ -1,8 +1,12 @@
|
|||||||
import {
|
import {
|
||||||
EditorState, Plugin, PluginKey, Transaction,
|
EditorState,
|
||||||
|
Plugin,
|
||||||
|
PluginKey,
|
||||||
|
Transaction,
|
||||||
} from 'prosemirror-state'
|
} from 'prosemirror-state'
|
||||||
import { EditorView } from 'prosemirror-view'
|
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 getNodeAttributes from './helpers/getNodeAttributes'
|
||||||
import getMarkAttributes from './helpers/getMarkAttributes'
|
import getMarkAttributes from './helpers/getMarkAttributes'
|
||||||
import isActive from './helpers/isActive'
|
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.
|
* Get attributes of the currently selected node.
|
||||||
*
|
*
|
||||||
* @param name Name of the node
|
* @param name Name of the node
|
||||||
*/
|
*/
|
||||||
public getNodeAttributes(name: string): Record<string, any> {
|
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)
|
return getNodeAttributes(this.state, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -346,6 +359,8 @@ export class Editor extends EventEmitter {
|
|||||||
* @param name Name of the mark
|
* @param name Name of the mark
|
||||||
*/
|
*/
|
||||||
public getMarkAttributes(name: string): Record<string, any> {
|
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)
|
return getMarkAttributes(this.state, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
27
packages/core/src/helpers/getAttributes.ts
Normal file
27
packages/core/src/helpers/getAttributes.ts
Normal 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 {}
|
||||||
|
}
|
||||||
@@ -21,6 +21,7 @@ export { default as generateHTML } from './helpers/generateHTML'
|
|||||||
export { default as generateJSON } from './helpers/generateJSON'
|
export { default as generateJSON } from './helpers/generateJSON'
|
||||||
export { default as getSchema } from './helpers/getSchema'
|
export { default as getSchema } from './helpers/getSchema'
|
||||||
export { default as getHTMLFromFragment } from './helpers/getHTMLFromFragment'
|
export { default as getHTMLFromFragment } from './helpers/getHTMLFromFragment'
|
||||||
|
export { default as getAttributes } from './helpers/getMarkAttributes'
|
||||||
export { default as getMarkAttributes } from './helpers/getMarkAttributes'
|
export { default as getMarkAttributes } from './helpers/getMarkAttributes'
|
||||||
export { default as getNodeAttributes } from './helpers/getNodeAttributes'
|
export { default as getNodeAttributes } from './helpers/getNodeAttributes'
|
||||||
export { default as getNodeType } from './helpers/getNodeType'
|
export { default as getNodeType } from './helpers/getNodeType'
|
||||||
|
|||||||
@@ -116,7 +116,7 @@ export const Link = Mark.create<LinkOptions>({
|
|||||||
key: new PluginKey('handleClickLink'),
|
key: new PluginKey('handleClickLink'),
|
||||||
props: {
|
props: {
|
||||||
handleClick: (view, pos, event) => {
|
handleClick: (view, pos, event) => {
|
||||||
const attrs = this.editor.getMarkAttributes('link')
|
const attrs = this.editor.getAttributes('link')
|
||||||
const link = (event.target as HTMLElement)?.closest('a')
|
const link = (event.target as HTMLElement)?.closest('a')
|
||||||
|
|
||||||
if (link && attrs.href) {
|
if (link && attrs.href) {
|
||||||
|
|||||||
Reference in New Issue
Block a user