feat: add editor.getAttributes, deprecate editor.getNodeAttributes and editor.getMarkAttributes
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
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 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'
|
||||
|
||||
Reference in New Issue
Block a user