1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-29 18:09:49 +02:00

feat: allow MarkTransforms methods to take a single mark (resolve #3175) (#3183)

This commit is contained in:
Wee
2019-12-02 02:49:29 +08:00
committed by Ian Storm Taylor
parent d99629517d
commit 434ce21cec
31 changed files with 69 additions and 58 deletions

View File

@@ -111,7 +111,7 @@ Editor.insertNodes(editor, [element], { at: path })
Editor.splitNodes(editor, { at: point })
// Add a mark to all the text in a range.
Editor.addMarks(editor, [mark], { at: range })
Editor.addMarks(editor, mark, { at: range })
```
The editor-specific helpers are the ones you'll use most often when working with Slate editors, so it pays to become very familiar with them.

View File

@@ -81,7 +81,7 @@ const App = () => {
// When "B" is pressed, add a bold mark to the text.
case 'b': {
event.preventDefault()
Editor.addMarks(editor, [{ type: 'bold' }])
Editor.addMarks(editor, { type: 'bold' })
break
}
}
@@ -154,7 +154,7 @@ const App = () => {
case 'b': {
event.preventDefault()
Editor.addMarks(editor, [{ type: 'bold' }])
Editor.addMarks(editor, { type: 'bold' })
break
}
}

View File

@@ -55,7 +55,7 @@ const App = () => {
case 'b': {
event.preventDefault()
Editor.addMarks(editor, [{ type: 'bold' }])
Editor.addMarks(editor, { type: 'bold' })
break
}
}

View File

@@ -89,7 +89,7 @@ export const createEditor = (): Editor => {
if (Command.isCoreCommand(command)) {
switch (command.type) {
case 'add_mark': {
Editor.addMarks(editor, [command.mark])
Editor.addMarks(editor, command.mark)
break
}

View File

@@ -7,7 +7,7 @@ export const MarkTransforms = {
addMarks(
editor: Editor,
marks: Mark[],
mark: Mark | Mark[],
options: {
at?: Location
hanging?: boolean
@@ -21,18 +21,19 @@ export const MarkTransforms = {
}
// De-dupe the marks being added to ensure the set is unique.
const marks = Array.isArray(mark) ? mark : [mark]
const set: Mark[] = []
for (const mark of marks) {
if (!Mark.exists(mark, set)) {
set.push(mark)
for (const m of marks) {
if (!Mark.exists(m, set)) {
set.push(m)
}
}
for (const [node, path] of Editor.texts(editor, { at })) {
for (const mark of set) {
if (!Mark.exists(mark, node.marks)) {
editor.apply({ type: 'add_mark', path, mark })
for (const m of set) {
if (!Mark.exists(m, node.marks)) {
editor.apply({ type: 'add_mark', path, mark: m })
}
}
}
@@ -41,7 +42,7 @@ export const MarkTransforms = {
removeMarks(
editor: Editor,
marks: Mark[],
mark: Mark | Mark[],
options: {
at?: Location
hanging?: boolean
@@ -51,9 +52,10 @@ export const MarkTransforms = {
const at = splitLocation(editor, options)
if (at) {
for (const [mark, i, node, path] of Editor.marks(editor, { at })) {
if (Mark.exists(mark, marks)) {
editor.apply({ type: 'remove_mark', path, mark })
const marks = Array.isArray(mark) ? mark : [mark]
for (const [m, i, node, path] of Editor.marks(editor, { at })) {
if (Mark.exists(m, marks)) {
editor.apply({ type: 'remove_mark', path, mark: m })
}
}
}
@@ -62,7 +64,7 @@ export const MarkTransforms = {
setMarks(
editor: Editor,
marks: Mark[],
mark: Mark | Mark[],
props: Partial<Mark>,
options: {
at?: Location
@@ -73,12 +75,13 @@ export const MarkTransforms = {
const at = splitLocation(editor, options)
if (at) {
for (const [mark, i, node, path] of Editor.marks(editor, { at })) {
if (Mark.exists(mark, marks)) {
const marks = Array.isArray(mark) ? mark : [mark]
for (const [m, i, node, path] of Editor.marks(editor, { at })) {
if (Mark.exists(m, marks)) {
const newProps = {}
for (const k in props) {
if (props[k] !== mark[k]) {
if (props[k] !== m[k]) {
newProps[k] = props[k]
}
}
@@ -87,7 +90,7 @@ export const MarkTransforms = {
editor.apply({
type: 'set_mark',
path,
properties: mark,
properties: m,
newProperties: newProps,
})
}

View File

@@ -4,7 +4,7 @@ import { Editor } from 'slate'
import { jsx } from '../../..'
export const run = editor => {
Editor.addMarks(editor, [{ key: 'a' }], { at: [0, 0] })
Editor.addMarks(editor, { key: 'a' }, { at: [0, 0] })
}
export const input = (

View File

@@ -4,12 +4,16 @@ import { Editor } from 'slate'
import { jsx } from '../../..'
export const run = editor => {
Editor.addMarks(editor, [{ key: 'a' }], {
at: {
anchor: { path: [0, 0], offset: 1 },
focus: { path: [0, 0], offset: 3 },
},
})
Editor.addMarks(
editor,
{ key: 'a' },
{
at: {
anchor: { path: [0, 0], offset: 1 },
focus: { path: [0, 0], offset: 3 },
},
}
)
}
export const input = (

View File

@@ -4,7 +4,7 @@ import { Editor } from 'slate'
import { jsx } from '../../..'
export const run = editor => {
Editor.addMarks(editor, [{ key: 'a' }])
Editor.addMarks(editor, { key: 'a' })
}
export const input = (

View File

@@ -4,7 +4,7 @@ import { Editor } from 'slate'
import { jsx } from '../../..'
export const run = editor => {
Editor.addMarks(editor, [{ key: 'a' }])
Editor.addMarks(editor, { key: 'a' })
}
export const input = (

View File

@@ -4,7 +4,7 @@ import { Editor } from 'slate'
import { jsx } from '../../..'
export const run = editor => {
Editor.addMarks(editor, [{ key: 'a' }])
Editor.addMarks(editor, { key: 'a' })
}
export const input = (

View File

@@ -4,7 +4,7 @@ import { Editor } from 'slate'
import { jsx } from '../../..'
export const run = editor => {
Editor.addMarks(editor, [{ key: 'a' }])
Editor.addMarks(editor, { key: 'a' })
}
export const input = (

View File

@@ -4,7 +4,7 @@ import { Editor } from 'slate'
import { jsx } from '../../..'
export const run = editor => {
Editor.addMarks(editor, [{ key: 'a' }])
Editor.addMarks(editor, { key: 'a' })
}
export const input = (

View File

@@ -4,7 +4,7 @@ import { Editor } from 'slate'
import { jsx } from '../../..'
export const run = editor => {
Editor.addMarks(editor, [{ key: 'a' }])
Editor.addMarks(editor, { key: 'a' })
}
export const input = (

View File

@@ -4,7 +4,7 @@ import { Editor } from 'slate'
import { jsx } from '../../..'
export const run = editor => {
Editor.addMarks(editor, [{ key: 'a' }])
Editor.addMarks(editor, { key: 'a' })
}
export const input = (

View File

@@ -4,7 +4,7 @@ import { Editor } from 'slate'
import { jsx } from '../../..'
export const run = editor => {
Editor.addMarks(editor, [{ key: 'a' }])
Editor.addMarks(editor, { key: 'a' })
}
export const input = (

View File

@@ -4,7 +4,7 @@ import { Editor } from 'slate'
import { jsx } from '../../..'
export const run = editor => {
Editor.addMarks(editor, [{ key: 'a' }])
Editor.addMarks(editor, { key: 'a' })
}
export const input = (

View File

@@ -4,7 +4,7 @@ import { Editor } from 'slate'
import { jsx } from '../../..'
export const run = editor => {
Editor.removeMarks(editor, [{ key: 'a' }], { at: [0, 0] })
Editor.removeMarks(editor, { key: 'a' }, { at: [0, 0] })
}
export const input = (

View File

@@ -4,12 +4,16 @@ import { Editor } from 'slate'
import { jsx } from '../../..'
export const run = editor => {
Editor.removeMarks(editor, [{ key: 'a' }], {
at: {
anchor: { path: [0, 1], offset: 0 },
focus: { path: [0, 1], offset: 2 },
},
})
Editor.removeMarks(
editor,
{ key: 'a' },
{
at: {
anchor: { path: [0, 1], offset: 0 },
focus: { path: [0, 1], offset: 2 },
},
}
)
}
export const input = (

View File

@@ -4,7 +4,7 @@ import { Editor } from 'slate'
import { jsx } from '../../..'
export const run = editor => {
Editor.removeMarks(editor, [{ key: 'a' }])
Editor.removeMarks(editor, { key: 'a' })
}
export const input = (

View File

@@ -4,7 +4,7 @@ import { Editor } from 'slate'
import { jsx } from '../../..'
export const run = editor => {
Editor.removeMarks(editor, [{ key: 'a' }])
Editor.removeMarks(editor, { key: 'a' })
}
export const input = (

View File

@@ -4,8 +4,8 @@ import { Editor } from 'slate'
import { jsx } from '../../..'
export const run = editor => {
Editor.addMarks(editor, [{ key: 'a' }])
Editor.removeMarks(editor, [{ key: 'a' }])
Editor.addMarks(editor, { key: 'a' })
Editor.removeMarks(editor, { key: 'a' })
Editor.insertText(editor, 'a')
}

View File

@@ -4,7 +4,7 @@ import { Editor } from 'slate'
import { jsx } from '../../..'
export const run = editor => {
Editor.removeMarks(editor, [{ key: 'a' }])
Editor.removeMarks(editor, { key: 'a' })
}
export const input = (

View File

@@ -4,7 +4,7 @@ import { Editor } from 'slate'
import { jsx } from '../../..'
export const run = editor => {
Editor.removeMarks(editor, [{ key: 'a' }])
Editor.removeMarks(editor, { key: 'a' })
}
export const input = (

View File

@@ -4,7 +4,7 @@ import { Editor } from 'slate'
import { jsx } from '../../..'
export const run = editor => {
Editor.removeMarks(editor, [{ key: 'a' }])
Editor.removeMarks(editor, { key: 'a' })
}
export const input = (

View File

@@ -4,7 +4,7 @@ import { Editor } from 'slate'
import { jsx } from '../../..'
export const run = editor => {
Editor.removeMarks(editor, [{ key: 'a' }])
Editor.removeMarks(editor, { key: 'a' })
}
export const input = (

View File

@@ -4,7 +4,7 @@ import { Editor } from 'slate'
import { jsx } from '../../..'
export const run = editor => {
Editor.removeMarks(editor, [{ key: 'a' }])
Editor.removeMarks(editor, { key: 'a' })
}
export const input = (

View File

@@ -4,7 +4,7 @@ import { Editor } from 'slate'
import { jsx } from '../../..'
export const run = editor => {
Editor.removeMarks(editor, [{ key: 'a' }])
Editor.removeMarks(editor, { key: 'a' })
}
export const input = (

View File

@@ -4,7 +4,7 @@ import { Editor } from 'slate'
import { jsx } from '../../..'
export const run = editor => {
Editor.removeMarks(editor, [{ key: 'a' }])
Editor.removeMarks(editor, { key: 'a' })
}
export const input = (

View File

@@ -4,7 +4,7 @@ import { Editor } from 'slate'
import { jsx } from '../../..'
export const run = editor => {
Editor.setMarks(editor, [{ existing: true }], { key: true }, { at: [0, 0] })
Editor.setMarks(editor, { existing: true }, { key: true }, { at: [0, 0] })
}
export const input = (

View File

@@ -6,7 +6,7 @@ import { jsx } from '../../..'
export const run = editor => {
Editor.setMarks(
editor,
[{ key: 'a' }],
{ key: 'a' },
{ thing: true },
{
at: {

View File

@@ -4,7 +4,7 @@ import { Editor } from 'slate'
import { jsx } from '../../..'
export const run = editor => {
Editor.setMarks(editor, [{ key: 'a' }], { thing: true })
Editor.setMarks(editor, { key: 'a' }, { thing: true })
}
export const input = (