add more commands

This commit is contained in:
Philipp Kühn
2020-09-20 23:44:38 +02:00
parent fbdc156981
commit f6270c0e0c
6 changed files with 27 additions and 27 deletions

View File

@@ -2,8 +2,9 @@
<div> <div>
<button @click="() => console.log(editor.focus())">focus</button> <button @click="() => console.log(editor.focus())">focus</button>
<button @click="() => console.log(editor.insertText('hello'))">insert</button> <button @click="() => console.log(editor.insertText('hello'))">insert</button>
<button @click="editor.chain().focus().insertText('wat').insertHTML('<p>2</p>').run()">chain</button> <button @click="editor.chain().focus().insertText('wat').insertHTML('<p>2</p>').run()">chain 1</button>
<button @click="editor.chain().insertText('1').focus().run()">chain</button> <button @click="editor.chain().insertText('1').focus().run()">chain 2</button>
<button @click="editor.chain().setContent('reset').insertText('1').clearContent().run()">setContent</button>
<editor-content :editor="editor" /> <editor-content :editor="editor" />
</div> </div>
</template> </template>

View File

@@ -1,6 +1,6 @@
import { Editor } from '../Editor' import { Command } from '../Editor'
type BlurCommand = () => Editor type BlurCommand = () => Command
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Editor {
@@ -8,9 +8,10 @@ declare module '../Editor' {
} }
} }
export default (next: Function, { view }: Editor) => () => { export const blur: BlurCommand = () => ({ editor }) => {
const element = view.dom as HTMLElement const element = editor.view.dom as HTMLElement
element.blur() element.blur()
next()
return true
} }

View File

@@ -1,6 +1,6 @@
import { Editor } from '../Editor' import { Command } from '../Editor'
type ClearContentCommand = (emitUpdate?: Boolean) => Editor type ClearContentCommand = (emitUpdate?: Boolean) => Command
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Editor {
@@ -8,7 +8,9 @@ declare module '../Editor' {
} }
} }
export default (next: Function, editor: Editor) => (emitUpdate = false) => { export const clearContent: ClearContentCommand = (emitUpdate = false) => ({ editor }) => {
// TODO: doesnt work, we have to re-use `tr`
editor.setContent('', emitUpdate) editor.setContent('', emitUpdate)
next()
return true
} }

View File

@@ -1,5 +1,5 @@
// export { default as blur } from './blur' export { blur } from './blur'
// export { default as clearContent } from './clearContent' export { clearContent } from './clearContent'
// export { default as deleteSelection } from './deleteSelection' // export { default as deleteSelection } from './deleteSelection'
export { focus } from './focus' export { focus } from './focus'
export { insertHTML } from './insertHTML' export { insertHTML } from './insertHTML'
@@ -10,7 +10,7 @@ export { insertText } from './insertText'
// export { default as replaceWithNode } from './replaceWithNode' // export { default as replaceWithNode } from './replaceWithNode'
// export { default as selectAll } from './selectAll' // export { default as selectAll } from './selectAll'
// export { default as selectParentNode } from './selectParentNode' // export { default as selectParentNode } from './selectParentNode'
// export { default as setContent } from './setContent' export { setContent } from './setContent'
// export { default as sinkListItem } from './sinkListItem' // export { default as sinkListItem } from './sinkListItem'
// export { default as splitListItem } from './splitListItem' // export { default as splitListItem } from './splitListItem'
// export { default as toggleMark } from './toggleMark' // export { default as toggleMark } from './toggleMark'

View File

@@ -25,14 +25,12 @@ function selectionToInsertionEnd(tr, startLen, bias) {
} }
export const insertHTML: InsertHTMLCommand = value => ({ tr, editor }) => { export const insertHTML: InsertHTMLCommand = value => ({ tr, editor }) => {
console.log({tr })
const { state } = editor const { state } = editor
const { selection } = tr const { selection } = tr
const element = elementFromString(value) const element = elementFromString(value)
const slice = DOMParser.fromSchema(state.schema).parseSlice(element) const slice = DOMParser.fromSchema(state.schema).parseSlice(element)
tr.insert(selection.anchor, slice.content) tr.insert(selection.anchor, slice.content)
// TODO: set correct bias by content
selectionToInsertionEnd(tr, tr.steps.length - 1, -1) selectionToInsertionEnd(tr, tr.steps.length - 1, -1)
return true return true

View File

@@ -1,11 +1,11 @@
import { Editor } from '../Editor' import { Command } from '../Editor'
import { TextSelection } from 'prosemirror-state' import { TextSelection } from 'prosemirror-state'
type SetContentCommand = ( type SetContentCommand = (
content: string, content: string,
emitUpdate?: Boolean, emitUpdate?: Boolean,
parseOptions?: any, parseOptions?: any,
) => Editor ) => Command
declare module '../Editor' { declare module '../Editor' {
interface Editor { interface Editor {
@@ -13,21 +13,19 @@ declare module '../Editor' {
} }
} }
export default (next: Function, editor: Editor) => (content: string, emitUpdate: Boolean = false, parseOptions = {}) => { export const setContent: SetContentCommand = (content = null, emitUpdate = false, parseOptions = {}) => ({ tr, editor }) => {
if (content === null) { if (content === null) {
next() return false
return
} }
const { view, state, createDocument } = editor const { createDocument } = editor
const { doc, tr } = state const { doc } = tr
const document = createDocument(content, parseOptions) const document = createDocument(content, parseOptions)
const selection = TextSelection.create(doc, 0, doc.content.size) const selection = TextSelection.create(doc, 0, doc.content.size)
const transaction = tr
.setSelection(selection) tr.setSelection(selection)
.replaceSelectionWith(document, false) .replaceSelectionWith(document, false)
.setMeta('preventUpdate', !emitUpdate) .setMeta('preventUpdate', !emitUpdate)
view.dispatch(transaction) return true
next()
} }