refactoring

This commit is contained in:
Philipp Kühn
2020-11-02 16:23:43 +01:00
parent eed82a57b6
commit 389937c32f
15 changed files with 61 additions and 40 deletions

View File

@@ -9,7 +9,7 @@
<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()"> <button @click="editor.chain().focus().resetNodeAttributes(['textAlign']).run()">
set default set default
</button> </button>
<editor-content :editor="editor" /> <editor-content :editor="editor" />

View File

@@ -5,7 +5,7 @@ import { createExtension } from '../Extension'
export const ClearNodes = createExtension({ export const ClearNodes = createExtension({
addCommands() { addCommands() {
return { return {
clearNodes: (): Command => ({ state, tr }) => { clearNodes: (): Command => ({ state, tr, dispatch }) => {
const { selection } = tr const { selection } = tr
const { from, to } = selection const { from, to } = selection
@@ -18,11 +18,11 @@ export const ClearNodes = createExtension({
if (nodeRange) { if (nodeRange) {
const targetLiftDepth = liftTarget(nodeRange) const targetLiftDepth = liftTarget(nodeRange)
if (node.type.isTextblock) { if (node.type.isTextblock && dispatch) {
tr.setNodeMarkup(nodeRange.start, state.schema.nodes.paragraph) tr.setNodeMarkup(nodeRange.start, state.schema.nodes.paragraph)
} }
if (targetLiftDepth || targetLiftDepth === 0) { if ((targetLiftDepth || targetLiftDepth === 0) && dispatch) {
tr.lift(nodeRange, targetLiftDepth) tr.lift(nodeRange, targetLiftDepth)
} }
} }

View File

@@ -40,7 +40,9 @@ function resolveSelection(editor: Editor, position: Position = null): ResolvedSe
export const Focus = createExtension({ export const Focus = createExtension({
addCommands() { addCommands() {
return { return {
focus: (position: Position = null): Command => ({ editor, view, tr }) => { focus: (position: Position = null): Command => ({
editor, view, tr, dispatch,
}) => {
if ((view.hasFocus() && position === null) || position === false) { if ((view.hasFocus() && position === null) || position === false) {
return true return true
} }
@@ -51,7 +53,10 @@ export const Focus = createExtension({
const resolvedEnd = minMax(to, 0, doc.content.size) const resolvedEnd = minMax(to, 0, doc.content.size)
const selection = TextSelection.create(doc, resolvedFrom, resolvedEnd) const selection = TextSelection.create(doc, resolvedFrom, resolvedEnd)
tr.setSelection(selection) if (dispatch) {
tr.setSelection(selection)
}
view.focus() view.focus()
return true return true

View File

@@ -11,7 +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 { ResetNodeAttributes } from './resetNodeAttributes'
export { SetNodeAttributes } from './setNodeAttributes' export { SetNodeAttributes } from './setNodeAttributes'
export { SetBlockType } from './setBlockType' export { SetBlockType } from './setBlockType'
export { SetContent } from './setContent' export { SetContent } from './setContent'

View File

@@ -21,13 +21,15 @@ function selectionToInsertionEnd(tr: Transaction, startLen: number, bias: number
export const InsertHTML = createExtension({ export const InsertHTML = createExtension({
addCommands() { addCommands() {
return { return {
insertHTML: (value: string): Command => ({ tr, state }) => { insertHTML: (value: string): Command => ({ tr, state, dispatch }) => {
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) if (dispatch) {
selectionToInsertionEnd(tr, tr.steps.length - 1, -1) tr.insert(selection.anchor, slice.content)
selectionToInsertionEnd(tr, tr.steps.length - 1, -1)
}
return true return true
}, },

View File

@@ -4,8 +4,10 @@ import { createExtension } from '../Extension'
export const InsertText = createExtension({ export const InsertText = createExtension({
addCommands() { addCommands() {
return { return {
insertText: (value: string): Command => ({ tr }) => { insertText: (value: string): Command => ({ tr, dispatch }) => {
tr.insertText(value) if (dispatch) {
tr.insertText(value)
}
return true return true
}, },

View File

@@ -7,7 +7,7 @@ import getMarkRange from '../utils/getMarkRange'
export const RemoveMark = createExtension({ export const RemoveMark = createExtension({
addCommands() { addCommands() {
return { return {
removeMark: (typeOrName: string | MarkType): Command => ({ tr, state }) => { removeMark: (typeOrName: string | MarkType): Command => ({ tr, state, dispatch }) => {
const { selection } = tr const { selection } = tr
const type = getMarkType(typeOrName, state.schema) const type = getMarkType(typeOrName, state.schema)
let { from, to } = selection let { from, to } = selection
@@ -22,7 +22,9 @@ export const RemoveMark = createExtension({
} }
} }
tr.removeMark(from, to, type) if (dispatch) {
tr.removeMark(from, to, type)
}
return true return true
}, },

View File

@@ -4,7 +4,7 @@ import { createExtension } from '../Extension'
export const RemoveMarks = createExtension({ export const RemoveMarks = createExtension({
addCommands() { addCommands() {
return { return {
removeMarks: (): Command => ({ tr, state }) => { removeMarks: (): Command => ({ tr, state, dispatch }) => {
const { selection } = tr const { selection } = tr
const { from, to, empty } = selection const { from, to, empty } = selection
@@ -12,11 +12,13 @@ export const RemoveMarks = createExtension({
return true return true
} }
Object if (dispatch) {
.entries(state.schema.marks) Object
.forEach(([, mark]) => { .entries(state.schema.marks)
tr.removeMark(from, to, mark as any) .forEach(([, mark]) => {
}) tr.removeMark(from, to, mark as any)
})
}
return true return true
}, },

View File

@@ -1,10 +1,10 @@
import { Command } from '../Editor' import { Command } from '../Editor'
import { createExtension } from '../Extension' import { createExtension } from '../Extension'
export const SetDefaultNodeAttributes = createExtension({ export const ResetNodeAttributes = createExtension({
addCommands() { addCommands() {
return { return {
setDefaultNodeAttributes: (attributeNames: string[] = []): Command => ({ tr, state }) => { resetNodeAttributes: (attributeNames: string[] = []): Command => ({ tr, state, dispatch }) => {
const { selection } = tr const { selection } = tr
const { from, to } = selection const { from, to } = selection
@@ -14,7 +14,7 @@ export const SetDefaultNodeAttributes = createExtension({
const attribute = node.type.spec.attrs?.[name] const attribute = node.type.spec.attrs?.[name]
const defaultValue = attribute?.default const defaultValue = attribute?.default
if (attribute && defaultValue !== undefined) { if (attribute && defaultValue !== undefined && dispatch) {
tr.setNodeMarkup(pos, undefined, { tr.setNodeMarkup(pos, undefined, {
[name]: defaultValue, [name]: defaultValue,
}) })
@@ -31,6 +31,6 @@ export const SetDefaultNodeAttributes = createExtension({
declare module '../Editor' { declare module '../Editor' {
interface AllExtensions { interface AllExtensions {
SetDefaultNodeAttributes: typeof SetDefaultNodeAttributes, ResetNodeAttributes: typeof ResetNodeAttributes,
} }
} }

View File

@@ -4,8 +4,10 @@ import { createExtension } from '../Extension'
export const ScrollIntoView = createExtension({ export const ScrollIntoView = createExtension({
addCommands() { addCommands() {
return { return {
scrollIntoView: (): Command => ({ tr }) => { scrollIntoView: (): Command => ({ tr, dispatch }) => {
tr.scrollIntoView() if (dispatch) {
tr.scrollIntoView()
}
return true return true
}, },

View File

@@ -5,15 +5,17 @@ import { createExtension } from '../Extension'
export const SetContent = createExtension({ export const SetContent = createExtension({
addCommands() { addCommands() {
return { return {
setContent: (content: string, emitUpdate: Boolean = false, parseOptions = {}): Command => ({ tr, editor }) => { setContent: (content: string, emitUpdate: Boolean = false, parseOptions = {}): Command => ({ tr, editor, dispatch }) => {
const { createDocument } = editor const { createDocument } = editor
const { doc } = tr 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)
tr.setSelection(selection) if (dispatch) {
.replaceSelectionWith(document, false) tr.setSelection(selection)
.setMeta('preventUpdate', !emitUpdate) .replaceSelectionWith(document, false)
.setMeta('preventUpdate', !emitUpdate)
}
return true return true
}, },

View File

@@ -4,12 +4,12 @@ import { createExtension } from '../Extension'
export const SetNodeAttributes = createExtension({ export const SetNodeAttributes = createExtension({
addCommands() { addCommands() {
return { return {
setNodeAttributes: (attributes: {}): Command => ({ tr, state }) => { setNodeAttributes: (attributes: {}): Command => ({ tr, state, dispatch }) => {
const { selection } = tr const { selection } = tr
const { from, to } = selection const { from, to } = selection
state.doc.nodesBetween(from, to, (node, pos) => { state.doc.nodesBetween(from, to, (node, pos) => {
if (!node.type.isText) { if (!node.type.isText && dispatch) {
tr.setNodeMarkup(pos, undefined, attributes) tr.setNodeMarkup(pos, undefined, attributes)
} }
}) })

View File

@@ -77,7 +77,7 @@ export const SplitBlock = createExtension({
} }
} }
dispatch(tr.scrollIntoView()) tr.scrollIntoView()
} }
return true return true

View File

@@ -31,10 +31,12 @@ export const ToggleList = createExtension({
} }
// change list type // change list type
if (isList(parentList.node.type.name, extensions) && listType.validContent(parentList.node.content)) { if (
if (dispatch) { isList(parentList.node.type.name, extensions)
tr.setNodeMarkup(parentList.pos, listType) && listType.validContent(parentList.node.content)
} && dispatch
) {
tr.setNodeMarkup(parentList.pos, listType)
return true return true
} }

View File

@@ -7,7 +7,7 @@ import getMarkRange from '../utils/getMarkRange'
export const UpdateMark = createExtension({ export const UpdateMark = createExtension({
addCommands() { addCommands() {
return { return {
updateMark: (typeOrName: string | MarkType, attrs: {}): Command => ({ tr, state }) => { updateMark: (typeOrName: string | MarkType, attrs: {}): Command => ({ tr, state, dispatch }) => {
const { selection, doc } = tr const { selection, doc } = tr
let { from, to } = selection let { from, to } = selection
const { $from, empty } = selection const { $from, empty } = selection
@@ -24,11 +24,13 @@ export const UpdateMark = createExtension({
const hasMark = doc.rangeHasMark(from, to, type) const hasMark = doc.rangeHasMark(from, to, type)
if (hasMark) { if (hasMark && dispatch) {
tr.removeMark(from, to, type) tr.removeMark(from, to, type)
} }
tr.addMark(from, to, type.create(attrs)) if (dispatch) {
tr.addMark(from, to, type.create(attrs))
}
return true return true
}, },