add setDefaultNodeAttributes
This commit is contained in:
@@ -9,6 +9,9 @@
|
|||||||
<button @click="editor.chain().focus().textAlign('right').run()">
|
<button @click="editor.chain().focus().textAlign('right').run()">
|
||||||
right
|
right
|
||||||
</button>
|
</button>
|
||||||
|
<button @click="editor.chain().focus().setDefaultNodeAttributes(['textAlign']).run()">
|
||||||
|
reset
|
||||||
|
</button>
|
||||||
<editor-content :editor="editor" />
|
<editor-content :editor="editor" />
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ export { RemoveMarks } from './removeMarks'
|
|||||||
export { ScrollIntoView } from './scrollIntoView'
|
export { ScrollIntoView } from './scrollIntoView'
|
||||||
export { SelectAll } from './selectAll'
|
export { SelectAll } from './selectAll'
|
||||||
export { SelectParentNode } from './selectParentNode'
|
export { SelectParentNode } from './selectParentNode'
|
||||||
|
export { SetDefaultNodeAttributes } from './setDefaultNodeAttributes'
|
||||||
export { SetNodeAttributes } from './setNodeAttributes'
|
export { SetNodeAttributes } from './setNodeAttributes'
|
||||||
export { SetBlockType } from './setBlockType'
|
export { SetBlockType } from './setBlockType'
|
||||||
export { SetContent } from './setContent'
|
export { SetContent } from './setContent'
|
||||||
|
|||||||
36
packages/core/src/extensions/setDefaultNodeAttributes.ts
Normal file
36
packages/core/src/extensions/setDefaultNodeAttributes.ts
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
import { Command } from '../Editor'
|
||||||
|
import { createExtension } from '../Extension'
|
||||||
|
|
||||||
|
export const SetDefaultNodeAttributes = createExtension({
|
||||||
|
addCommands() {
|
||||||
|
return {
|
||||||
|
setDefaultNodeAttributes: (attributeNames: string[] = []): Command => ({ tr, state }) => {
|
||||||
|
const { selection } = tr
|
||||||
|
const { from, to } = selection
|
||||||
|
|
||||||
|
state.doc.nodesBetween(from, to, (node, pos) => {
|
||||||
|
if (!node.type.isText) {
|
||||||
|
attributeNames.forEach(name => {
|
||||||
|
const attribute = node.type.spec.attrs?.[name]
|
||||||
|
const defaultValue = attribute?.default
|
||||||
|
|
||||||
|
if (attribute && defaultValue !== undefined) {
|
||||||
|
tr.setNodeMarkup(pos, undefined, {
|
||||||
|
[name]: defaultValue,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return true
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
declare module '../Editor' {
|
||||||
|
interface AllExtensions {
|
||||||
|
SetDefaultNodeAttributes: typeof SetDefaultNodeAttributes,
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user