1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-02-24 09:13:24 +01:00

Merge branch 'master' of github.com:ianstormtaylor/slate

This commit is contained in:
Benjamin Kniffler 2016-09-14 02:26:10 +02:00
commit a9a9dfdb01
11 changed files with 194 additions and 84 deletions

View File

@ -47,8 +47,16 @@ Transform methods can either operate on the [`Document`](./document.md), the [`S
- [`moveTo`](#moveto)
- [`move{Direction}`](#movedirection)
- [Node Transforms](#node-transforms)
- [`removeNodeByKey`](#removeNodeByKey)
- [`setNodeByKey`](#setNodeByKey)
- [`addMarkByKey`](#addmarkbykey)
- [`insertNodeByKey`](#insertnodebykey)
- [`insertTextByKey`](#inserttextbykey)
- [`moveNodeByKey`](#movenodebykey)
- [`removeMarkByKey`](#removemarkbykey)
- [`removeNodeByKey`](#removenodebykey)
- [`removeTextByKey`](#removetextbykey)
- [`setMarkByKey`](#setmarkbykey)
- [`setNodeByKey`](#setnodebykey)
- [`splitNodeByKey`](#splitnodebykey)
- [Document Transforms](#document-transforms)
- [`deleteAtRange`](#deleteatrange)
- [`deleteBackwardAtRange`](#deletebackwardatrange)
@ -258,17 +266,57 @@ Move the current selection to a selection with merged `properties`. The `propert
## 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(key: String) => Transform`
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(key: String, properties: Object) => Transform` <br/>
`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.
### `splitNodeByKey`
`splitNodeByKey(key: String, offset: Number) => Transform`
Split a node by its `key` at an `offset`.
## 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.
## History Transforms
### `redo`
`redo() => State`
`redo() => Transform`
Move forward one step in the history.
### `undo`
`undo() => State`
`undo() => Transform`
Move backward one step in the history.

View File

@ -1,7 +1,7 @@
{
"name": "slate",
"description": "A completely customizable framework for building rich text editors.",
"version": "0.14.2",
"version": "0.14.3",
"license": "MIT",
"repository": "git://github.com/ianstormtaylor/slate.git",
"main": "./lib/index.js",

View File

@ -932,46 +932,6 @@ const Node = {
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`.
*
@ -1129,34 +1089,6 @@ const 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.
*

View File

@ -62,8 +62,8 @@ export function deleteAtRange(transform, range) {
let { state } = transform
let { document } = state
let ancestor = document.getCommonAncestor(startKey, endKey)
const startChild = ancestor.getHighestChild(startKey)
const endChild = ancestor.getHighestChild(endKey)
let startChild = ancestor.getHighestChild(startKey)
let endChild = ancestor.getHighestChild(endKey)
const startOff = startChild.getOffset(startKey) + startOffset
const endOff = endChild.getOffset(endKey) + endOffset
@ -73,16 +73,16 @@ export function deleteAtRange(transform, range) {
state = transform.state
document = state.document
ancestor = document.getCommonAncestor(startKey, endKey)
const startBlock = document.getClosestBlock(startKey)
const endBlock = document.getClosestBlock(document.getNextText(endKey))
const startIndex = ancestor.nodes.indexOf(startBlock)
const endIndex = ancestor.nodes.indexOf(endBlock)
const endLonelyParent = ancestor.getHighestChild(endBlock, (parent) => {
return parent.nodes.size == 1
})
startChild = ancestor.getHighestChild(startBlock)
endChild = ancestor.getHighestChild(endBlock)
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)
})
@ -92,7 +92,7 @@ export function deleteAtRange(transform, range) {
transform.moveNodeByKey(child.key, newKey, newIndex)
})
transform.removeNodeByKey(endLonelyParent.key)
transform.removeNodeByKey(endChild.key)
transform.normalizeDocument()
return transform
}
@ -304,7 +304,7 @@ export function insertFragmentAtRange(transform, range, fragment) {
fragment = fragment.removeDescendant(lonelyChild)
fragment.nodes.forEach((node, i) => {
const newIndex = startIndex + i + 2
const newIndex = startIndex + i + 1
transform.insertNodeByKey(parent.key, newIndex, node)
})
}

View File

@ -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()
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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()
}

View File

@ -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

View File

@ -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