add heading command
This commit is contained in:
@@ -16,6 +16,12 @@
|
|||||||
<button @click="editor.focus().italic()" :class="{ 'is-active': editor.isActive('italic') }">
|
<button @click="editor.focus().italic()" :class="{ 'is-active': editor.isActive('italic') }">
|
||||||
italic
|
italic
|
||||||
</button>
|
</button>
|
||||||
|
<button @click="editor.focus().heading({ level: 1 })" :class="{ 'is-active': editor.isActive('heading', { level: 1 }) }">
|
||||||
|
h1
|
||||||
|
</button>
|
||||||
|
<button @click="editor.focus().heading({ level: 2 })" :class="{ 'is-active': editor.isActive('heading', { level: 2 }) }">
|
||||||
|
h2
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<editor-content :editor="editor" />
|
<editor-content :editor="editor" />
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -71,6 +71,7 @@ export class Editor extends EventEmitter {
|
|||||||
this.registerCommand('setContent', require('./commands/setContent').default)
|
this.registerCommand('setContent', require('./commands/setContent').default)
|
||||||
this.registerCommand('clearContent', require('./commands/clearContent').default)
|
this.registerCommand('clearContent', require('./commands/clearContent').default)
|
||||||
this.registerCommand('removeMarks', require('./commands/removeMarks').default)
|
this.registerCommand('removeMarks', require('./commands/removeMarks').default)
|
||||||
|
this.registerCommand('toggleBlockType', require('./commands/toggleBlockType').default)
|
||||||
|
|
||||||
if (this.options.injectCSS) {
|
if (this.options.injectCSS) {
|
||||||
require('./style.css')
|
require('./style.css')
|
||||||
|
|||||||
30
packages/core/src/commands/toggleBlockType.ts
Normal file
30
packages/core/src/commands/toggleBlockType.ts
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import { NodeType } from 'prosemirror-model'
|
||||||
|
import { setBlockType } from 'prosemirror-commands'
|
||||||
|
import { Editor } from '../Editor'
|
||||||
|
import nodeIsActive from '../utils/nodeIsActive'
|
||||||
|
|
||||||
|
declare module '../Editor' {
|
||||||
|
interface Editor {
|
||||||
|
toggleBlockType(type: NodeType, toggleType: NodeType, attrs?: {}): Editor,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function toggleBlockType(
|
||||||
|
next: Function,
|
||||||
|
editor: Editor,
|
||||||
|
type: NodeType,
|
||||||
|
toggleType: NodeType,
|
||||||
|
attrs?: {},
|
||||||
|
): void {
|
||||||
|
const { view, state } = editor
|
||||||
|
const isActive = nodeIsActive(state, type, attrs)
|
||||||
|
|
||||||
|
if (isActive) {
|
||||||
|
setBlockType(toggleType)(view.state, view.dispatch)
|
||||||
|
next()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
setBlockType(type, attrs)(view.state, view.dispatch)
|
||||||
|
next()
|
||||||
|
}
|
||||||
@@ -43,13 +43,13 @@ export default class Heading extends Node {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// commands(): CommandSpec {
|
commands(): CommandSpec {
|
||||||
// return {
|
return {
|
||||||
// heading: (next, { view }) => {
|
heading: (next, editor, attrs) => {
|
||||||
// toggleBlockType(this.type)(view.state, view.dispatch)
|
editor.toggleBlockType(this.type, editor.schema.nodes.paragraph, attrs)
|
||||||
// next()
|
next()
|
||||||
// },
|
},
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user