mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-09-02 19:52:32 +02:00
Merge branch 'master' of github.com:ianstormtaylor/slate
This commit is contained in:
@@ -47,8 +47,16 @@ Transform methods can either operate on the [`Document`](./document.md), the [`S
|
|||||||
- [`moveTo`](#moveto)
|
- [`moveTo`](#moveto)
|
||||||
- [`move{Direction}`](#movedirection)
|
- [`move{Direction}`](#movedirection)
|
||||||
- [Node Transforms](#node-transforms)
|
- [Node Transforms](#node-transforms)
|
||||||
- [`removeNodeByKey`](#removeNodeByKey)
|
- [`addMarkByKey`](#addmarkbykey)
|
||||||
- [`setNodeByKey`](#setNodeByKey)
|
- [`insertNodeByKey`](#insertnodebykey)
|
||||||
|
- [`insertTextByKey`](#inserttextbykey)
|
||||||
|
- [`moveNodeByKey`](#movenodebykey)
|
||||||
|
- [`removeMarkByKey`](#removemarkbykey)
|
||||||
|
- [`removeNodeByKey`](#removenodebykey)
|
||||||
|
- [`removeTextByKey`](#removetextbykey)
|
||||||
|
- [`setMarkByKey`](#setmarkbykey)
|
||||||
|
- [`setNodeByKey`](#setnodebykey)
|
||||||
|
- [`splitNodeByKey`](#splitnodebykey)
|
||||||
- [Document Transforms](#document-transforms)
|
- [Document Transforms](#document-transforms)
|
||||||
- [`deleteAtRange`](#deleteatrange)
|
- [`deleteAtRange`](#deleteatrange)
|
||||||
- [`deleteBackwardAtRange`](#deletebackwardatrange)
|
- [`deleteBackwardAtRange`](#deletebackwardatrange)
|
||||||
@@ -258,17 +266,57 @@ Move the current selection to a selection with merged `properties`. The `propert
|
|||||||
|
|
||||||
## Node Transforms
|
## Node Transforms
|
||||||
|
|
||||||
|
### `addMarkByKey`
|
||||||
|
`addMarkByKey(key: String, offset: Number, length: Number, mark: Mark) => Transform`
|
||||||
|
|
||||||
|
Add a `mark` to `length` characters starting at an `offset` in a [`Node`](./node.md) by its `key`.
|
||||||
|
|
||||||
|
### `insertNodeByKey`
|
||||||
|
`insertNodeByKey(key: String, index: Number, node: Node) => Transform`
|
||||||
|
|
||||||
|
Insert a `node` at `index` inside a parent [`Node`](./node.md) by its `key`.
|
||||||
|
|
||||||
|
### `insertTextByKey`
|
||||||
|
`insertTextByKey(key: String, offset: Number, text: String, [marks: Set]) => Transform`
|
||||||
|
|
||||||
|
Insert `text` at an `offset` in a [`Node`](./node.md) with optional `marks`.
|
||||||
|
|
||||||
|
### `moveNodeByKey`
|
||||||
|
`moveNodeByKey(key: String, newKey: String, newIndex: Number) => Transform`
|
||||||
|
|
||||||
|
Move a [`Node`](./node.md) by its `key` to a new parent node with its `newKey` and at a `newIndex`.
|
||||||
|
|
||||||
|
### `removeMarkByKey`
|
||||||
|
`removeMarkByKey(key: String, offset: Number, length: Number, mark: Mark) => Transform`
|
||||||
|
|
||||||
|
Remove a `mark` from `length` characters starting at an `offset` in a [`Node`](./node.md) by its `key`.
|
||||||
|
|
||||||
### `removeNodeByKey`
|
### `removeNodeByKey`
|
||||||
`removeNodeByKey(key: String) => Transform`
|
`removeNodeByKey(key: String) => Transform`
|
||||||
|
|
||||||
Remove a [`Node`](./node.md) from the document by its `key`.
|
Remove a [`Node`](./node.md) from the document by its `key`.
|
||||||
|
|
||||||
|
### `removeTextByKey`
|
||||||
|
`removeTextByKey(key: String, offset: Number, length: Number) => Transform`
|
||||||
|
|
||||||
|
Remove `length` characters of text starting at an `offset` in a [`Node`](./node.md) by its `key`.
|
||||||
|
|
||||||
|
### `setMarkByKey`
|
||||||
|
`setMarkByKey(key: String, offset: Number, length: Number, mark: Mark, properties: Object) => Transform`
|
||||||
|
|
||||||
|
Set a dictionary of `properties` on a [`mark`](./mark.md) on a [`Node`](./node.md) by its `key`.
|
||||||
|
|
||||||
### `setNodeByKey`
|
### `setNodeByKey`
|
||||||
`setNodeByKey(key: String, properties: Object) => Transform` <br/>
|
`setNodeByKey(key: String, properties: Object) => Transform` <br/>
|
||||||
`setNodeByKey(key: String, type: String) => Transform`
|
`setNodeByKey(key: String, type: String) => Transform`
|
||||||
|
|
||||||
Set a dictionary of `properties` on a [`Node`](./node.md) by its `key`. For convenience, you can pass a `type` string or `properties` object.
|
Set a dictionary of `properties` on a [`Node`](./node.md) by its `key`. For convenience, you can pass a `type` string or `properties` object.
|
||||||
|
|
||||||
|
### `splitNodeByKey`
|
||||||
|
`splitNodeByKey(key: String, offset: Number) => Transform`
|
||||||
|
|
||||||
|
Split a node by its `key` at an `offset`.
|
||||||
|
|
||||||
|
|
||||||
## Document Transforms
|
## Document Transforms
|
||||||
|
|
||||||
@@ -382,14 +430,15 @@ Wrap the [`Inline`](./inline.md) nodes in a `range` with a new [`Inline`](./inli
|
|||||||
|
|
||||||
Surround the text in a `range` with `prefix` and `suffix` strings. If the `suffix` is ommitted, the `prefix` will be used instead.
|
Surround the text in a `range` with `prefix` and `suffix` strings. If the `suffix` is ommitted, the `prefix` will be used instead.
|
||||||
|
|
||||||
|
|
||||||
## History Transforms
|
## History Transforms
|
||||||
|
|
||||||
### `redo`
|
### `redo`
|
||||||
`redo() => State`
|
`redo() => Transform`
|
||||||
|
|
||||||
Move forward one step in the history.
|
Move forward one step in the history.
|
||||||
|
|
||||||
### `undo`
|
### `undo`
|
||||||
`undo() => State`
|
`undo() => Transform`
|
||||||
|
|
||||||
Move backward one step in the history.
|
Move backward one step in the history.
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "slate",
|
"name": "slate",
|
||||||
"description": "A completely customizable framework for building rich text editors.",
|
"description": "A completely customizable framework for building rich text editors.",
|
||||||
"version": "0.14.2",
|
"version": "0.14.3",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"repository": "git://github.com/ianstormtaylor/slate.git",
|
"repository": "git://github.com/ianstormtaylor/slate.git",
|
||||||
"main": "./lib/index.js",
|
"main": "./lib/index.js",
|
||||||
|
@@ -932,46 +932,6 @@ const Node = {
|
|||||||
return !!this.getClosest(key, parent => parent.isVoid)
|
return !!this.getClosest(key, parent => parent.isVoid)
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
|
||||||
* Insert child `nodes` after child by `key`.
|
|
||||||
*
|
|
||||||
* @param {String or Node} key
|
|
||||||
* @param {List} nodes
|
|
||||||
* @return {Node} node
|
|
||||||
*/
|
|
||||||
|
|
||||||
insertChildrenAfter(key, nodes) {
|
|
||||||
const child = this.assertChild(key)
|
|
||||||
const index = this.nodes.indexOf(child)
|
|
||||||
|
|
||||||
nodes = this.nodes
|
|
||||||
.slice(0, index + 1)
|
|
||||||
.concat(nodes)
|
|
||||||
.concat(this.nodes.slice(index + 1))
|
|
||||||
|
|
||||||
return this.merge({ nodes })
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Insert child `nodes` before child by `key`.
|
|
||||||
*
|
|
||||||
* @param {String or Node} key
|
|
||||||
* @param {List} nodes
|
|
||||||
* @return {Node} node
|
|
||||||
*/
|
|
||||||
|
|
||||||
insertChildrenBefore(key, nodes) {
|
|
||||||
const child = this.assertChild(key)
|
|
||||||
const index = this.nodes.indexOf(child)
|
|
||||||
|
|
||||||
nodes = this.nodes
|
|
||||||
.slice(0, index)
|
|
||||||
.concat(nodes)
|
|
||||||
.concat(this.nodes.slice(index))
|
|
||||||
|
|
||||||
return this.merge({ nodes })
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Insert a `node` at `index`.
|
* Insert a `node` at `index`.
|
||||||
*
|
*
|
||||||
@@ -1129,34 +1089,6 @@ const Node = {
|
|||||||
return node
|
return node
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove children after a child by `key`.
|
|
||||||
*
|
|
||||||
* @param {String or Node} key
|
|
||||||
* @return {Node} node
|
|
||||||
*/
|
|
||||||
|
|
||||||
removeChildrenAfter(key) {
|
|
||||||
const child = this.assertChild(key)
|
|
||||||
const index = this.nodes.indexOf(child)
|
|
||||||
const nodes = this.nodes.slice(0, index + 1)
|
|
||||||
return this.merge({ nodes })
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove children after a child by `key`, including the child.
|
|
||||||
*
|
|
||||||
* @param {String or Node} key
|
|
||||||
* @return {Node} node
|
|
||||||
*/
|
|
||||||
|
|
||||||
removeChildrenAfterIncluding(key) {
|
|
||||||
const child = this.assertChild(key)
|
|
||||||
const index = this.nodes.indexOf(child)
|
|
||||||
const nodes = this.nodes.slice(0, index)
|
|
||||||
return this.merge({ nodes })
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove a `node` from the children node map.
|
* Remove a `node` from the children node map.
|
||||||
*
|
*
|
||||||
|
@@ -62,8 +62,8 @@ export function deleteAtRange(transform, range) {
|
|||||||
let { state } = transform
|
let { state } = transform
|
||||||
let { document } = state
|
let { document } = state
|
||||||
let ancestor = document.getCommonAncestor(startKey, endKey)
|
let ancestor = document.getCommonAncestor(startKey, endKey)
|
||||||
const startChild = ancestor.getHighestChild(startKey)
|
let startChild = ancestor.getHighestChild(startKey)
|
||||||
const endChild = ancestor.getHighestChild(endKey)
|
let endChild = ancestor.getHighestChild(endKey)
|
||||||
const startOff = startChild.getOffset(startKey) + startOffset
|
const startOff = startChild.getOffset(startKey) + startOffset
|
||||||
const endOff = endChild.getOffset(endKey) + endOffset
|
const endOff = endChild.getOffset(endKey) + endOffset
|
||||||
|
|
||||||
@@ -73,16 +73,16 @@ export function deleteAtRange(transform, range) {
|
|||||||
state = transform.state
|
state = transform.state
|
||||||
document = state.document
|
document = state.document
|
||||||
ancestor = document.getCommonAncestor(startKey, endKey)
|
ancestor = document.getCommonAncestor(startKey, endKey)
|
||||||
|
|
||||||
const startBlock = document.getClosestBlock(startKey)
|
const startBlock = document.getClosestBlock(startKey)
|
||||||
const endBlock = document.getClosestBlock(document.getNextText(endKey))
|
const endBlock = document.getClosestBlock(document.getNextText(endKey))
|
||||||
const startIndex = ancestor.nodes.indexOf(startBlock)
|
startChild = ancestor.getHighestChild(startBlock)
|
||||||
const endIndex = ancestor.nodes.indexOf(endBlock)
|
endChild = ancestor.getHighestChild(endBlock)
|
||||||
const endLonelyParent = ancestor.getHighestChild(endBlock, (parent) => {
|
|
||||||
return parent.nodes.size == 1
|
|
||||||
})
|
|
||||||
|
|
||||||
ancestor.nodes.slice(startIndex + 1, endIndex).forEach((child) => {
|
const startIndex = ancestor.nodes.indexOf(startChild)
|
||||||
|
const endIndex = ancestor.nodes.indexOf(endChild)
|
||||||
|
const middles = ancestor.nodes.slice(startIndex + 1, endIndex)
|
||||||
|
|
||||||
|
middles.forEach((child) => {
|
||||||
transform.removeNodeByKey(child.key)
|
transform.removeNodeByKey(child.key)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -92,7 +92,7 @@ export function deleteAtRange(transform, range) {
|
|||||||
transform.moveNodeByKey(child.key, newKey, newIndex)
|
transform.moveNodeByKey(child.key, newKey, newIndex)
|
||||||
})
|
})
|
||||||
|
|
||||||
transform.removeNodeByKey(endLonelyParent.key)
|
transform.removeNodeByKey(endChild.key)
|
||||||
transform.normalizeDocument()
|
transform.normalizeDocument()
|
||||||
return transform
|
return transform
|
||||||
}
|
}
|
||||||
@@ -304,7 +304,7 @@ export function insertFragmentAtRange(transform, range, fragment) {
|
|||||||
fragment = fragment.removeDescendant(lonelyChild)
|
fragment = fragment.removeDescendant(lonelyChild)
|
||||||
|
|
||||||
fragment.nodes.forEach((node, i) => {
|
fragment.nodes.forEach((node, i) => {
|
||||||
const newIndex = startIndex + i + 2
|
const newIndex = startIndex + i + 1
|
||||||
transform.insertNodeByKey(parent.key, newIndex, node)
|
transform.insertNodeByKey(parent.key, newIndex, node)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,18 @@
|
|||||||
|
|
||||||
|
export default function (state) {
|
||||||
|
const { document, selection } = state
|
||||||
|
const texts = document.getTexts()
|
||||||
|
const second = texts.get(1)
|
||||||
|
const third = texts.get(2)
|
||||||
|
const range = selection.merge({
|
||||||
|
anchorKey: second.key,
|
||||||
|
anchorOffset: second.length,
|
||||||
|
focusKey: third.key,
|
||||||
|
focusOffset: 0
|
||||||
|
})
|
||||||
|
|
||||||
|
return state
|
||||||
|
.transform()
|
||||||
|
.deleteAtRange(range)
|
||||||
|
.apply()
|
||||||
|
}
|
@@ -0,0 +1,20 @@
|
|||||||
|
|
||||||
|
nodes:
|
||||||
|
- kind: block
|
||||||
|
type: list
|
||||||
|
nodes:
|
||||||
|
- kind: block
|
||||||
|
type: item
|
||||||
|
nodes:
|
||||||
|
- kind: text
|
||||||
|
text: one
|
||||||
|
- kind: block
|
||||||
|
type: item
|
||||||
|
nodes:
|
||||||
|
- kind: text
|
||||||
|
text: two
|
||||||
|
- kind: block
|
||||||
|
type: paragraph
|
||||||
|
nodes:
|
||||||
|
- kind: text
|
||||||
|
text: three
|
@@ -0,0 +1,15 @@
|
|||||||
|
|
||||||
|
nodes:
|
||||||
|
- kind: block
|
||||||
|
type: list
|
||||||
|
nodes:
|
||||||
|
- kind: block
|
||||||
|
type: item
|
||||||
|
nodes:
|
||||||
|
- kind: text
|
||||||
|
text: one
|
||||||
|
- kind: block
|
||||||
|
type: item
|
||||||
|
nodes:
|
||||||
|
- kind: text
|
||||||
|
text: twothree
|
@@ -0,0 +1,14 @@
|
|||||||
|
|
||||||
|
nodes:
|
||||||
|
- kind: block
|
||||||
|
type: paragraph
|
||||||
|
nodes:
|
||||||
|
- kind: text
|
||||||
|
ranges:
|
||||||
|
- text: fragment one
|
||||||
|
- kind: block
|
||||||
|
type: paragraph
|
||||||
|
nodes:
|
||||||
|
- kind: text
|
||||||
|
ranges:
|
||||||
|
- text: fragment two
|
@@ -0,0 +1,25 @@
|
|||||||
|
|
||||||
|
import path from 'path'
|
||||||
|
import readMetadata from 'read-metadata'
|
||||||
|
import { Raw } from '../../../../../..'
|
||||||
|
|
||||||
|
export default function (state) {
|
||||||
|
const file = path.resolve(__dirname, 'fragment.yaml')
|
||||||
|
const raw = readMetadata.sync(file)
|
||||||
|
const fragment = Raw.deserialize(raw, { terse: true }).document
|
||||||
|
|
||||||
|
const { document, selection } = state
|
||||||
|
const texts = document.getTexts()
|
||||||
|
const first = texts.first()
|
||||||
|
const range = selection.merge({
|
||||||
|
anchorKey: first.key,
|
||||||
|
anchorOffset: 2,
|
||||||
|
focusKey: first.key,
|
||||||
|
focusOffset: 2
|
||||||
|
})
|
||||||
|
|
||||||
|
return state
|
||||||
|
.transform()
|
||||||
|
.insertFragmentAtRange(range, fragment)
|
||||||
|
.apply()
|
||||||
|
}
|
@@ -0,0 +1,17 @@
|
|||||||
|
|
||||||
|
nodes:
|
||||||
|
- kind: block
|
||||||
|
type: list-item
|
||||||
|
nodes:
|
||||||
|
- kind: block
|
||||||
|
type: paragraph
|
||||||
|
nodes:
|
||||||
|
- kind: text
|
||||||
|
ranges:
|
||||||
|
- text: first
|
||||||
|
- kind: block
|
||||||
|
type: paragraph
|
||||||
|
nodes:
|
||||||
|
- kind: text
|
||||||
|
ranges:
|
||||||
|
- text: second
|
@@ -0,0 +1,20 @@
|
|||||||
|
|
||||||
|
nodes:
|
||||||
|
- kind: block
|
||||||
|
type: list-item
|
||||||
|
nodes:
|
||||||
|
- kind: block
|
||||||
|
type: paragraph
|
||||||
|
nodes:
|
||||||
|
- kind: text
|
||||||
|
text: fifragment one
|
||||||
|
- kind: block
|
||||||
|
type: paragraph
|
||||||
|
nodes:
|
||||||
|
- kind: text
|
||||||
|
text: fragment tworst
|
||||||
|
- kind: block
|
||||||
|
type: paragraph
|
||||||
|
nodes:
|
||||||
|
- kind: text
|
||||||
|
text: second
|
Reference in New Issue
Block a user