mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-22 23:12:52 +02:00
tighten linting rules
This commit is contained in:
28
.eslintrc
28
.eslintrc
@@ -18,7 +18,10 @@
|
|||||||
"node": true
|
"node": true
|
||||||
},
|
},
|
||||||
"rules": {
|
"rules": {
|
||||||
|
"arrow-parens": ["error", "as-needed", { "requireForBlockBody": true }],
|
||||||
|
"arrow-spacing": "error",
|
||||||
"block-spacing": "error",
|
"block-spacing": "error",
|
||||||
|
"capitalized-comments": ["error", "always", { "ignoreConsecutiveComments": true }],
|
||||||
"comma-dangle": ["error", "only-multiline"],
|
"comma-dangle": ["error", "only-multiline"],
|
||||||
"comma-spacing": ["error", { "before": false, "after": true }],
|
"comma-spacing": ["error", { "before": false, "after": true }],
|
||||||
"comma-style": ["error", "last"],
|
"comma-style": ["error", "last"],
|
||||||
@@ -28,8 +31,12 @@
|
|||||||
"dot-location": ["error", "property"],
|
"dot-location": ["error", "property"],
|
||||||
"dot-notation": ["error", { "allowKeywords": true }],
|
"dot-notation": ["error", { "allowKeywords": true }],
|
||||||
"eol-last": "error",
|
"eol-last": "error",
|
||||||
|
"func-call-spacing": ["error", "never"],
|
||||||
"func-style": ["error", "declaration"],
|
"func-style": ["error", "declaration"],
|
||||||
|
"import/default": "error",
|
||||||
"import/export": "error",
|
"import/export": "error",
|
||||||
|
"import/first": "error",
|
||||||
|
"import/named": "error",
|
||||||
"import/namespace": "error",
|
"import/namespace": "error",
|
||||||
"import/newline-after-import": "error",
|
"import/newline-after-import": "error",
|
||||||
"import/no-deprecated": "error",
|
"import/no-deprecated": "error",
|
||||||
@@ -39,8 +46,11 @@
|
|||||||
"import/no-named-as-default-member": "error",
|
"import/no-named-as-default-member": "error",
|
||||||
"import/no-unresolved": "error",
|
"import/no-unresolved": "error",
|
||||||
"indent": ["error", 2, { "SwitchCase": 1, "MemberExpression": 1 }],
|
"indent": ["error", 2, { "SwitchCase": 1, "MemberExpression": 1 }],
|
||||||
|
"jsx-quotes": ["error", "prefer-double"],
|
||||||
"key-spacing": ["error", { "beforeColon": false, "afterColon": true }],
|
"key-spacing": ["error", { "beforeColon": false, "afterColon": true }],
|
||||||
"lines-around-comment": ["error", { "beforeBlockComment": true, "afterBlockComment": true }],
|
"keyword-spacing": ["error", { "before": true, "after": true }],
|
||||||
|
"linebreak-style": "error",
|
||||||
|
"lines-around-comment": ["error", { "beforeBlockComment": true, "afterBlockComment": true, "allowBlockStart": true, "allowObjectStart": true, "allowArrayStart": true }],
|
||||||
"new-parens": "error",
|
"new-parens": "error",
|
||||||
"no-array-constructor": "error",
|
"no-array-constructor": "error",
|
||||||
"no-class-assign": "error",
|
"no-class-assign": "error",
|
||||||
@@ -56,8 +66,10 @@
|
|||||||
"no-empty-pattern": "error",
|
"no-empty-pattern": "error",
|
||||||
"no-ex-assign": "error",
|
"no-ex-assign": "error",
|
||||||
"no-extend-native": "error",
|
"no-extend-native": "error",
|
||||||
|
"no-extra-boolean-cast": "error",
|
||||||
"no-func-assign": "error",
|
"no-func-assign": "error",
|
||||||
"no-invalid-regexp": "error",
|
"no-invalid-regexp": "error",
|
||||||
|
"no-lonely-if": "error",
|
||||||
"no-mixed-spaces-and-tabs": ["error", false],
|
"no-mixed-spaces-and-tabs": ["error", false],
|
||||||
"no-multi-spaces": "error",
|
"no-multi-spaces": "error",
|
||||||
"no-multiple-empty-lines": ["error", { "max": 2, "maxEOF": 1 }],
|
"no-multiple-empty-lines": ["error", { "max": 2, "maxEOF": 1 }],
|
||||||
@@ -65,19 +77,24 @@
|
|||||||
"no-negated-in-lhs": "error",
|
"no-negated-in-lhs": "error",
|
||||||
"no-new-object": "error",
|
"no-new-object": "error",
|
||||||
"no-new-symbol": "error",
|
"no-new-symbol": "error",
|
||||||
|
"no-path-concat": "error",
|
||||||
"no-redeclare": "error",
|
"no-redeclare": "error",
|
||||||
"no-regex-spaces": "error",
|
"no-regex-spaces": "error",
|
||||||
"no-sequences": "error",
|
"no-sequences": "error",
|
||||||
"no-shadow": "error",
|
"no-shadow": "error",
|
||||||
"no-shadow-restricted-names": "error",
|
"no-shadow-restricted-names": "error",
|
||||||
"no-spaced-func": "error",
|
"no-spaced-func": "error",
|
||||||
|
"no-tabs": "error",
|
||||||
|
"no-this-before-super": "error",
|
||||||
"no-throw-literal": "error",
|
"no-throw-literal": "error",
|
||||||
"no-trailing-spaces": "error",
|
"no-trailing-spaces": "error",
|
||||||
"no-undef": "error",
|
"no-undef": "error",
|
||||||
|
"no-unneeded-ternary": "error",
|
||||||
"no-unreachable": "error",
|
"no-unreachable": "error",
|
||||||
"no-unsafe-finally": "error",
|
"no-unsafe-finally": "error",
|
||||||
|
"no-unsafe-negation": "error",
|
||||||
"no-unused-expressions": "error",
|
"no-unused-expressions": "error",
|
||||||
"no-unused-vars": ["warn", { "vars": "all", "args": "none" }],
|
"no-unused-vars": ["error", { "vars": "all", "args": "none" }],
|
||||||
"no-useless-call": "error",
|
"no-useless-call": "error",
|
||||||
"no-useless-computed-key": "error",
|
"no-useless-computed-key": "error",
|
||||||
"no-useless-constructor": "error",
|
"no-useless-constructor": "error",
|
||||||
@@ -86,12 +103,17 @@
|
|||||||
"no-void": "error",
|
"no-void": "error",
|
||||||
"no-whitespace-before-property": "error",
|
"no-whitespace-before-property": "error",
|
||||||
"no-with": "error",
|
"no-with": "error",
|
||||||
|
"object-curly-spacing": ["error", "always", { "objectsInObjects": false }],
|
||||||
"object-property-newline": ["error", { "allowMultiplePropertiesPerLine": true }],
|
"object-property-newline": ["error", { "allowMultiplePropertiesPerLine": true }],
|
||||||
"object-shorthand": ["error", "always"],
|
"object-shorthand": ["error", "always"],
|
||||||
|
"operator-linebreak": ["error", "after", { "overrides": { "?": "ignore", ":": "ignore" }}],
|
||||||
|
"padded-blocks": ["error", { "blocks": "never", "classes": "always" }],
|
||||||
"prefer-arrow-callback": "error",
|
"prefer-arrow-callback": "error",
|
||||||
|
"prefer-const": ["error", { "destructuring": "all", "ignoreReadBeforeAssign": true }],
|
||||||
"prefer-rest-params": "error",
|
"prefer-rest-params": "error",
|
||||||
"prefer-spread": "error",
|
"prefer-spread": "error",
|
||||||
"prefer-template": "error",
|
"prefer-template": "error",
|
||||||
|
"quotes": ["error", "single", { "allowTemplateLiterals": true }],
|
||||||
"radix": "error",
|
"radix": "error",
|
||||||
"react/jsx-boolean-value": ["error", "never"],
|
"react/jsx-boolean-value": ["error", "never"],
|
||||||
"react/jsx-closing-bracket-location": "error",
|
"react/jsx-closing-bracket-location": "error",
|
||||||
@@ -101,6 +123,7 @@
|
|||||||
"react/jsx-key": "error",
|
"react/jsx-key": "error",
|
||||||
"react/jsx-no-bind": "error",
|
"react/jsx-no-bind": "error",
|
||||||
"react/jsx-no-duplicate-props": "error",
|
"react/jsx-no-duplicate-props": "error",
|
||||||
|
"react/jsx-no-target-blank": "error",
|
||||||
"react/jsx-no-undef": "error",
|
"react/jsx-no-undef": "error",
|
||||||
"react/jsx-space-before-closing": ["error", "always"],
|
"react/jsx-space-before-closing": ["error", "always"],
|
||||||
"react/jsx-uses-react": "error",
|
"react/jsx-uses-react": "error",
|
||||||
@@ -123,6 +146,7 @@
|
|||||||
"space-unary-ops": ["error", { "words": true, "nonwords": false }],
|
"space-unary-ops": ["error", { "words": true, "nonwords": false }],
|
||||||
"spaced-comment": ["error", "always", { "exceptions": ["-"]}],
|
"spaced-comment": ["error", "always", { "exceptions": ["-"]}],
|
||||||
"template-curly-spacing": "error",
|
"template-curly-spacing": "error",
|
||||||
|
"template-tag-spacing": ["error", "never"],
|
||||||
"unicode-bom": ["error", "never"],
|
"unicode-bom": ["error", "never"],
|
||||||
"use-isnan": "error",
|
"use-isnan": "error",
|
||||||
"valid-jsdoc": ["error", { "prefer": { "return": "returns" }, "requireReturn": false }],
|
"valid-jsdoc": ["error", { "prefer": { "return": "returns" }, "requireReturn": false }],
|
||||||
|
@@ -130,7 +130,7 @@ class AutoMarkdown extends React.Component {
|
|||||||
if (type == 'list-item' && startBlock.type == 'list-item') return
|
if (type == 'list-item' && startBlock.type == 'list-item') return
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
|
|
||||||
let transform = state
|
const transform = state
|
||||||
.transform()
|
.transform()
|
||||||
.setBlock(type)
|
.setBlock(type)
|
||||||
|
|
||||||
@@ -161,7 +161,7 @@ class AutoMarkdown extends React.Component {
|
|||||||
if (startBlock.type == 'paragraph') return
|
if (startBlock.type == 'paragraph') return
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
|
|
||||||
let transform = state
|
const transform = state
|
||||||
.transform()
|
.transform()
|
||||||
.setBlock('paragraph')
|
.setBlock('paragraph')
|
||||||
|
|
||||||
|
@@ -153,7 +153,7 @@ class CodeHighlighting extends React.Component {
|
|||||||
const { startBlock } = state
|
const { startBlock } = state
|
||||||
if (startBlock.type != 'code') return
|
if (startBlock.type != 'code') return
|
||||||
|
|
||||||
let transform = state.transform()
|
const transform = state.transform()
|
||||||
if (state.isExpanded) transform.delete()
|
if (state.isExpanded) transform.delete()
|
||||||
transform.insertText('\n')
|
transform.insertText('\n')
|
||||||
|
|
||||||
|
@@ -166,7 +166,7 @@ class RichText extends React.Component {
|
|||||||
const isActive = this.hasBlock(type)
|
const isActive = this.hasBlock(type)
|
||||||
let { state } = this.state
|
let { state } = this.state
|
||||||
|
|
||||||
let transform = state
|
const transform = state
|
||||||
.transform()
|
.transform()
|
||||||
.setBlock(isActive ? 'paragraph' : type)
|
.setBlock(isActive ? 'paragraph' : type)
|
||||||
|
|
||||||
|
@@ -2,8 +2,8 @@
|
|||||||
import { Editor, Raw } from '../..'
|
import { Editor, Raw } from '../..'
|
||||||
import Portal from 'react-portal'
|
import Portal from 'react-portal'
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import position from 'selection-position'
|
|
||||||
import initialState from './state.json'
|
import initialState from './state.json'
|
||||||
|
import position from 'selection-position'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Define a schema.
|
* Define a schema.
|
||||||
|
@@ -122,7 +122,7 @@ class Links extends React.Component {
|
|||||||
if (data.type != 'text' && data.type != 'html') return
|
if (data.type != 'text' && data.type != 'html') return
|
||||||
if (!isUrl(data.text)) return
|
if (!isUrl(data.text)) return
|
||||||
|
|
||||||
let transform = state.transform()
|
const transform = state.transform()
|
||||||
|
|
||||||
if (this.hasLinks()) {
|
if (this.hasLinks()) {
|
||||||
transform.unwrapInline('link')
|
transform.unwrapInline('link')
|
||||||
|
@@ -163,7 +163,7 @@ class RichText extends React.Component {
|
|||||||
onClickBlock = (e, type) => {
|
onClickBlock = (e, type) => {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
let { state } = this.state
|
let { state } = this.state
|
||||||
let transform = state.transform()
|
const transform = state.transform()
|
||||||
const { document } = state
|
const { document } = state
|
||||||
|
|
||||||
// Handle everything but list buttons.
|
// Handle everything but list buttons.
|
||||||
|
@@ -11,7 +11,7 @@ import initialState from './state.json'
|
|||||||
|
|
||||||
const schema = {
|
const schema = {
|
||||||
nodes: {
|
nodes: {
|
||||||
'block-quote': (props) => <blockquote {...props.attributes}>{props.children}</blockquote>,
|
'block-quote': props => <blockquote {...props.attributes}>{props.children}</blockquote>,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -402,9 +402,8 @@ class Content extends React.Component {
|
|||||||
range.setStart(nativeEvent.rangeParent, nativeEvent.rangeOffset)
|
range.setStart(nativeEvent.rangeParent, nativeEvent.rangeOffset)
|
||||||
}
|
}
|
||||||
|
|
||||||
const startNode = range.startContainer
|
const { startContainer, startOffset } = range
|
||||||
const startOffset = range.startOffset
|
const point = this.getPoint(startContainer, startOffset)
|
||||||
const point = this.getPoint(startNode, startOffset)
|
|
||||||
if (!point) return
|
if (!point) return
|
||||||
|
|
||||||
const target = Selection.create({
|
const target = Selection.create({
|
||||||
@@ -616,7 +615,7 @@ class Content extends React.Component {
|
|||||||
|
|
||||||
const window = getWindow(event.target)
|
const window = getWindow(event.target)
|
||||||
const { state } = this.props
|
const { state } = this.props
|
||||||
let { document, selection } = state
|
const { document, selection } = state
|
||||||
const native = window.getSelection()
|
const native = window.getSelection()
|
||||||
const data = {}
|
const data = {}
|
||||||
|
|
||||||
@@ -704,7 +703,7 @@ class Content extends React.Component {
|
|||||||
.map(node => this.renderNode(node))
|
.map(node => this.renderNode(node))
|
||||||
.toArray()
|
.toArray()
|
||||||
|
|
||||||
let style = {
|
const style = {
|
||||||
// Prevent the default outline styles.
|
// Prevent the default outline styles.
|
||||||
outline: 'none',
|
outline: 'none',
|
||||||
// Preserve adjacent whitespace and new lines.
|
// Preserve adjacent whitespace and new lines.
|
||||||
|
@@ -166,43 +166,40 @@ class Leaf extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise we need to set the selection across two different leaves.
|
// Otherwise we need to set the selection across two different leaves.
|
||||||
else {
|
// If the selection is forward, we can set things in sequence. In the
|
||||||
// If the selection is forward, we can set things in sequence. In the
|
// first leaf to render, reset the selection and set the new start. And
|
||||||
// first leaf to render, reset the selection and set the new start. And
|
// then in the second leaf to render, extend to the new end.
|
||||||
// then in the second leaf to render, extend to the new end.
|
else if (selection.isForward) {
|
||||||
if (selection.isForward) {
|
if (hasAnchor) {
|
||||||
if (hasAnchor) {
|
native.removeAllRanges()
|
||||||
native.removeAllRanges()
|
const range = window.document.createRange()
|
||||||
const range = window.document.createRange()
|
range.setStart(el, anchorOffset)
|
||||||
range.setStart(el, anchorOffset)
|
native.addRange(range)
|
||||||
native.addRange(range)
|
} else if (hasFocus) {
|
||||||
} else if (hasFocus) {
|
native.extend(el, focusOffset)
|
||||||
native.extend(el, focusOffset)
|
focus()
|
||||||
focus()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Otherwise, if the selection is backward, we need to hack the order a bit.
|
// Otherwise, if the selection is backward, we need to hack the order a bit.
|
||||||
// In the first leaf to render, set a phony start anchor to store the true
|
// In the first leaf to render, set a phony start anchor to store the true
|
||||||
// end position. And then in the second leaf to render, set the start and
|
// end position. And then in the second leaf to render, set the start and
|
||||||
// extend the end to the stored value.
|
// extend the end to the stored value.
|
||||||
else {
|
else if (hasFocus) {
|
||||||
if (hasFocus) {
|
native.removeAllRanges()
|
||||||
native.removeAllRanges()
|
const range = window.document.createRange()
|
||||||
const range = window.document.createRange()
|
range.setStart(el, focusOffset)
|
||||||
range.setStart(el, focusOffset)
|
native.addRange(range)
|
||||||
native.addRange(range)
|
}
|
||||||
} else if (hasAnchor) {
|
else if (hasAnchor) {
|
||||||
const endNode = native.focusNode
|
const endNode = native.focusNode
|
||||||
const endOffset = native.focusOffset
|
const endOffset = native.focusOffset
|
||||||
native.removeAllRanges()
|
native.removeAllRanges()
|
||||||
const range = window.document.createRange()
|
const range = window.document.createRange()
|
||||||
range.setStart(el, anchorOffset)
|
range.setStart(el, anchorOffset)
|
||||||
native.addRange(range)
|
native.addRange(range)
|
||||||
native.extend(endNode, endOffset)
|
native.extend(endNode, endOffset)
|
||||||
focus()
|
focus()
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.debug('updateSelection', { selection })
|
this.debug('updateSelection', { selection })
|
||||||
@@ -305,6 +302,7 @@ class Leaf extends React.Component {
|
|||||||
)
|
)
|
||||||
}, children)
|
}, children)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -62,7 +62,7 @@ class Node extends React.Component {
|
|||||||
debug = (message, ...args) => {
|
debug = (message, ...args) => {
|
||||||
const { node } = this.props
|
const { node } = this.props
|
||||||
const { key, kind, type } = node
|
const { key, kind, type } = node
|
||||||
let id = kind == 'text' ? `${key} (${kind})` : `${key} (${type})`
|
const id = kind == 'text' ? `${key} (${kind})` : `${key} (${type})`
|
||||||
debug(message, `${id}`, ...args)
|
debug(message, `${id}`, ...args)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -349,8 +349,7 @@ class Node extends React.Component {
|
|||||||
|
|
||||||
renderLeaf = (ranges, range, index, offset) => {
|
renderLeaf = (ranges, range, index, offset) => {
|
||||||
const { node, parent, schema, state, editor } = this.props
|
const { node, parent, schema, state, editor } = this.props
|
||||||
const text = range.text
|
const { text, marks } = range
|
||||||
const marks = range.marks
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Leaf
|
<Leaf
|
||||||
|
@@ -73,7 +73,7 @@ class Placeholder extends React.Component {
|
|||||||
let { style } = this.props
|
let { style } = this.props
|
||||||
|
|
||||||
if (typeof children === 'string' && style == null && className == null) {
|
if (typeof children === 'string' && style == null && className == null) {
|
||||||
style = { opacity: '0.333'}
|
style = { opacity: '0.333' }
|
||||||
} else if (style == null) {
|
} else if (style == null) {
|
||||||
style = {}
|
style = {}
|
||||||
}
|
}
|
||||||
|
@@ -47,7 +47,7 @@ class Void extends React.Component {
|
|||||||
debug = (message, ...args) => {
|
debug = (message, ...args) => {
|
||||||
const { node } = this.props
|
const { node } = this.props
|
||||||
const { key, type } = node
|
const { key, type } = node
|
||||||
let id = `${key} (${type})`
|
const id = `${key} (${type})`
|
||||||
debug(message, `${id}`, ...args)
|
debug(message, `${id}`, ...args)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -55,7 +55,7 @@ class Character extends new Record(DEFAULTS) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
static createListFromText(string, marks) {
|
static createListFromText(string, marks) {
|
||||||
const chars = string.split('').map(text => { return { text, marks } })
|
const chars = string.split('').map((text) => { return { text, marks } })
|
||||||
const list = Character.createList(chars)
|
const list = Character.createList(chars)
|
||||||
return list
|
return list
|
||||||
}
|
}
|
||||||
|
@@ -30,7 +30,7 @@ const Node = {
|
|||||||
getKeys() {
|
getKeys() {
|
||||||
const keys = []
|
const keys = []
|
||||||
|
|
||||||
this.forEachDescendant(desc => {
|
this.forEachDescendant((desc) => {
|
||||||
keys.push(desc.key)
|
keys.push(desc.key)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -160,7 +160,7 @@ const Node = {
|
|||||||
|
|
||||||
let descendantFound = null
|
let descendantFound = null
|
||||||
|
|
||||||
this.nodes.find(node => {
|
this.nodes.find((node) => {
|
||||||
if (node.kind != 'text') {
|
if (node.kind != 'text') {
|
||||||
descendantFound = node.findDescendant(iterator)
|
descendantFound = node.findDescendant(iterator)
|
||||||
return descendantFound
|
return descendantFound
|
||||||
@@ -182,7 +182,7 @@ const Node = {
|
|||||||
findDescendantDeep(iterator) {
|
findDescendantDeep(iterator) {
|
||||||
let found
|
let found
|
||||||
|
|
||||||
this.forEachDescendant(node => {
|
this.forEachDescendant((node) => {
|
||||||
if (iterator(node)) {
|
if (iterator(node)) {
|
||||||
found = node
|
found = node
|
||||||
return false
|
return false
|
||||||
@@ -339,7 +339,7 @@ const Node = {
|
|||||||
|
|
||||||
getClosest(key, iterator) {
|
getClosest(key, iterator) {
|
||||||
key = Normalize.key(key)
|
key = Normalize.key(key)
|
||||||
let ancestors = this.getAncestors(key)
|
const ancestors = this.getAncestors(key)
|
||||||
if (!ancestors) {
|
if (!ancestors) {
|
||||||
throw new Error(`Could not find a descendant node with key "${key}".`)
|
throw new Error(`Could not find a descendant node with key "${key}".`)
|
||||||
}
|
}
|
||||||
@@ -478,7 +478,7 @@ const Node = {
|
|||||||
_getDescendant(key) {
|
_getDescendant(key) {
|
||||||
let descendantFound = null
|
let descendantFound = null
|
||||||
|
|
||||||
const found = this.nodes.find(node => {
|
const found = this.nodes.find((node) => {
|
||||||
if (node.key === key) {
|
if (node.key === key) {
|
||||||
return node
|
return node
|
||||||
} else if (node.kind !== 'text') {
|
} else if (node.kind !== 'text') {
|
||||||
@@ -526,7 +526,7 @@ const Node = {
|
|||||||
|
|
||||||
let sorted
|
let sorted
|
||||||
|
|
||||||
this.forEachDescendant(n => {
|
this.forEachDescendant((n) => {
|
||||||
if (n.key === key1) {
|
if (n.key === key1) {
|
||||||
sorted = true
|
sorted = true
|
||||||
return false
|
return false
|
||||||
@@ -606,7 +606,7 @@ const Node = {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
getFurthest(key, iterator) {
|
getFurthest(key, iterator) {
|
||||||
let ancestors = this.getAncestors(key)
|
const ancestors = this.getAncestors(key)
|
||||||
if (!ancestors) {
|
if (!ancestors) {
|
||||||
key = Normalize.key(key)
|
key = Normalize.key(key)
|
||||||
throw new Error(`Could not find a descendant node with key "${key}".`)
|
throw new Error(`Could not find a descendant node with key "${key}".`)
|
||||||
@@ -647,7 +647,7 @@ const Node = {
|
|||||||
|
|
||||||
getHighestChild(key) {
|
getHighestChild(key) {
|
||||||
key = Normalize.key(key)
|
key = Normalize.key(key)
|
||||||
return this.nodes.find(node => {
|
return this.nodes.find((node) => {
|
||||||
if (node.key == key) return true
|
if (node.key == key) return true
|
||||||
if (node.kind == 'text') return false
|
if (node.kind == 'text') return false
|
||||||
return node.hasDescendant(key)
|
return node.hasDescendant(key)
|
||||||
@@ -890,7 +890,7 @@ const Node = {
|
|||||||
|
|
||||||
if (key == this.key) return []
|
if (key == this.key) return []
|
||||||
|
|
||||||
let path = []
|
const path = []
|
||||||
let childKey = key
|
let childKey = key
|
||||||
let parent
|
let parent
|
||||||
|
|
||||||
@@ -1024,11 +1024,8 @@ const Node = {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
getTextDirection() {
|
getTextDirection() {
|
||||||
const text = this.text
|
const dir = direction(this.text)
|
||||||
const dir = direction(text)
|
return dir == 'neutral' ? undefined : dir
|
||||||
return dir == 'neutral'
|
|
||||||
? undefined
|
|
||||||
: dir
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1160,7 +1157,7 @@ const Node = {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
insertNode(index, node) {
|
insertNode(index, node) {
|
||||||
let keys = this.getKeys()
|
const keys = this.getKeys()
|
||||||
|
|
||||||
if (keys.contains(node.key)) {
|
if (keys.contains(node.key)) {
|
||||||
node = node.regenerateKey()
|
node = node.regenerateKey()
|
||||||
@@ -1220,7 +1217,7 @@ const Node = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
const size = first.nodes.size
|
const { size } = first.nodes
|
||||||
second.nodes.forEach((child, i) => {
|
second.nodes.forEach((child, i) => {
|
||||||
first = first.insertNode(size + i, child)
|
first = first.insertNode(size + i, child)
|
||||||
})
|
})
|
||||||
@@ -1246,10 +1243,10 @@ const Node = {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
mapChildren(iterator) {
|
mapChildren(iterator) {
|
||||||
let nodes = this.nodes
|
let { nodes } = this
|
||||||
|
|
||||||
nodes.forEach((node, i) => {
|
nodes.forEach((node, i) => {
|
||||||
let ret = iterator(node, i, this.nodes)
|
const ret = iterator(node, i, this.nodes)
|
||||||
if (ret != node) nodes = nodes.set(ret.key, ret)
|
if (ret != node) nodes = nodes.set(ret.key, ret)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -1265,7 +1262,7 @@ const Node = {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
mapDescendants(iterator) {
|
mapDescendants(iterator) {
|
||||||
let nodes = this.nodes
|
let { nodes } = this
|
||||||
|
|
||||||
nodes.forEach((node, i) => {
|
nodes.forEach((node, i) => {
|
||||||
let ret = node
|
let ret = node
|
||||||
@@ -1338,7 +1335,7 @@ const Node = {
|
|||||||
|
|
||||||
splitNode(path, offset) {
|
splitNode(path, offset) {
|
||||||
let base = this
|
let base = this
|
||||||
let node = base.assertPath(path)
|
const node = base.assertPath(path)
|
||||||
let parent = base.getParent(node.key)
|
let parent = base.getParent(node.key)
|
||||||
const isParent = base == parent
|
const isParent = base == parent
|
||||||
const index = parent.nodes.indexOf(node)
|
const index = parent.nodes.indexOf(node)
|
||||||
@@ -1396,7 +1393,7 @@ const Node = {
|
|||||||
|
|
||||||
splitNodeAfter(path, count) {
|
splitNodeAfter(path, count) {
|
||||||
let base = this
|
let base = this
|
||||||
let node = base.assertPath(path)
|
const node = base.assertPath(path)
|
||||||
if (node.kind === 'text') throw new Error('Cannot split text node at index. Use Node.splitNode at offset instead')
|
if (node.kind === 'text') throw new Error('Cannot split text node at index. Use Node.splitNode at offset instead')
|
||||||
const { nodes } = node
|
const { nodes } = node
|
||||||
|
|
||||||
@@ -1429,7 +1426,7 @@ const Node = {
|
|||||||
|
|
||||||
splitBlockAtRange(range, height = 1) {
|
splitBlockAtRange(range, height = 1) {
|
||||||
const { startKey, startOffset } = range
|
const { startKey, startOffset } = range
|
||||||
let base = this
|
const base = this
|
||||||
let node = base.assertDescendant(startKey)
|
let node = base.assertDescendant(startKey)
|
||||||
let parent = base.getClosestBlock(node.key)
|
let parent = base.getClosestBlock(node.key)
|
||||||
let offset = startOffset
|
let offset = startOffset
|
||||||
@@ -1456,7 +1453,7 @@ const Node = {
|
|||||||
updateDescendant(node) {
|
updateDescendant(node) {
|
||||||
let found = false
|
let found = false
|
||||||
|
|
||||||
const result = this.mapDescendants(d => {
|
const result = this.mapDescendants((d) => {
|
||||||
if (d.key == node.key) {
|
if (d.key == node.key) {
|
||||||
found = true
|
found = true
|
||||||
return node
|
return node
|
||||||
|
@@ -64,6 +64,7 @@ class Range extends new Record(DEFAULTS) {
|
|||||||
})
|
})
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -295,7 +295,7 @@ class Selection extends new Record(DEFAULTS) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
normalize(node) {
|
normalize(node) {
|
||||||
let selection = this
|
const selection = this
|
||||||
let { anchorKey, anchorOffset, focusKey, focusOffset, isBackward } = selection
|
let { anchorKey, anchorOffset, focusKey, focusOffset, isBackward } = selection
|
||||||
|
|
||||||
// If the selection isn't formed yet or is malformed, ensure that it is
|
// If the selection isn't formed yet or is malformed, ensure that it is
|
||||||
@@ -322,8 +322,8 @@ class Selection extends new Record(DEFAULTS) {
|
|||||||
// If the anchor node isn't a text node, match it to one.
|
// If the anchor node isn't a text node, match it to one.
|
||||||
if (anchorNode.kind != 'text') {
|
if (anchorNode.kind != 'text') {
|
||||||
warn('The selection anchor was set to a Node that is not a Text node. This should not happen and can degrade performance. The node in question was:', anchorNode)
|
warn('The selection anchor was set to a Node that is not a Text node. This should not happen and can degrade performance. The node in question was:', anchorNode)
|
||||||
let anchorText = anchorNode.getTextAtOffset(anchorOffset)
|
const anchorText = anchorNode.getTextAtOffset(anchorOffset)
|
||||||
let offset = anchorNode.getOffset(anchorText.key)
|
const offset = anchorNode.getOffset(anchorText.key)
|
||||||
anchorOffset = anchorOffset - offset
|
anchorOffset = anchorOffset - offset
|
||||||
anchorNode = anchorText
|
anchorNode = anchorText
|
||||||
}
|
}
|
||||||
@@ -331,8 +331,8 @@ class Selection extends new Record(DEFAULTS) {
|
|||||||
// If the focus node isn't a text node, match it to one.
|
// If the focus node isn't a text node, match it to one.
|
||||||
if (focusNode.kind != 'text') {
|
if (focusNode.kind != 'text') {
|
||||||
warn('The selection focus was set to a Node that is not a Text node. This should not happen and can degrade performance. The node in question was:', focusNode)
|
warn('The selection focus was set to a Node that is not a Text node. This should not happen and can degrade performance. The node in question was:', focusNode)
|
||||||
let focusText = focusNode.getTextAtOffset(focusOffset)
|
const focusText = focusNode.getTextAtOffset(focusOffset)
|
||||||
let offset = focusNode.getOffset(focusText.key)
|
const offset = focusNode.getOffset(focusText.key)
|
||||||
focusOffset = focusOffset - offset
|
focusOffset = focusOffset - offset
|
||||||
focusNode = focusText
|
focusNode = focusText
|
||||||
}
|
}
|
||||||
|
@@ -48,7 +48,7 @@ class State extends new Record(DEFAULTS) {
|
|||||||
static create(properties = {}) {
|
static create(properties = {}) {
|
||||||
if (properties instanceof State) return properties
|
if (properties instanceof State) return properties
|
||||||
|
|
||||||
let document = Document.create(properties.document)
|
const document = Document.create(properties.document)
|
||||||
let selection = Selection.create(properties.selection)
|
let selection = Selection.create(properties.selection)
|
||||||
|
|
||||||
if (selection.isUnset) {
|
if (selection.isUnset) {
|
||||||
|
@@ -212,7 +212,7 @@ class Text extends new Record(DEFAULTS) {
|
|||||||
|
|
||||||
getRanges(decorators = []) {
|
getRanges(decorators = []) {
|
||||||
const list = new List()
|
const list = new List()
|
||||||
let characters = this.getDecorations(decorators)
|
const characters = this.getDecorations(decorators)
|
||||||
|
|
||||||
// If there are no characters, return one empty range.
|
// If there are no characters, return one empty range.
|
||||||
if (characters.size == 0) {
|
if (characters.size == 0) {
|
||||||
@@ -327,8 +327,8 @@ class Text extends new Record(DEFAULTS) {
|
|||||||
|
|
||||||
removeText(index, length) {
|
removeText(index, length) {
|
||||||
let { characters } = this
|
let { characters } = this
|
||||||
let start = index
|
const start = index
|
||||||
let end = index + length
|
const end = index + length
|
||||||
characters = characters.filterNot((char, i) => start <= i && i < end)
|
characters = characters.filterNot((char, i) => start <= i && i < end)
|
||||||
return this.merge({ characters })
|
return this.merge({ characters })
|
||||||
}
|
}
|
||||||
|
@@ -52,15 +52,15 @@ class Transform {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
apply(options = {}) {
|
apply(options = {}) {
|
||||||
let transform = this
|
const transform = this
|
||||||
let { merge, save, isNative = false } = options
|
let { merge, save, isNative = false } = options
|
||||||
|
|
||||||
// Ensure that the selection is normalized.
|
// Ensure that the selection is normalized.
|
||||||
transform.normalizeSelection()
|
transform.normalizeSelection()
|
||||||
|
|
||||||
let { state, operations } = transform
|
const { state, operations } = transform
|
||||||
let { history } = state
|
const { history } = state
|
||||||
let { undos } = history
|
const { undos } = history
|
||||||
const previous = undos.peek()
|
const previous = undos.peek()
|
||||||
|
|
||||||
// If there are no operations, abort early.
|
// If there are no operations, abort early.
|
||||||
|
@@ -317,7 +317,7 @@ function Plugin(options = {}) {
|
|||||||
: selection.endOffset)
|
: selection.endOffset)
|
||||||
}
|
}
|
||||||
|
|
||||||
let transform = state.transform()
|
const transform = state.transform()
|
||||||
|
|
||||||
if (isInternal) transform.delete()
|
if (isInternal) transform.delete()
|
||||||
|
|
||||||
@@ -340,7 +340,7 @@ function Plugin(options = {}) {
|
|||||||
debug('onDropText', { data })
|
debug('onDropText', { data })
|
||||||
|
|
||||||
const { text, target } = data
|
const { text, target } = data
|
||||||
let transform = state
|
const transform = state
|
||||||
.transform()
|
.transform()
|
||||||
.moveTo(target)
|
.moveTo(target)
|
||||||
|
|
||||||
@@ -784,7 +784,7 @@ function Plugin(options = {}) {
|
|||||||
function onPasteText(e, data, state) {
|
function onPasteText(e, data, state) {
|
||||||
debug('onPasteText', { data })
|
debug('onPasteText', { data })
|
||||||
|
|
||||||
let transform = state.transform()
|
const transform = state.transform()
|
||||||
|
|
||||||
data.text
|
data.text
|
||||||
.split('\n')
|
.split('\n')
|
||||||
@@ -836,9 +836,9 @@ function Plugin(options = {}) {
|
|||||||
state={props.state}
|
state={props.state}
|
||||||
style={placeholderStyle}
|
style={placeholderStyle}
|
||||||
>
|
>
|
||||||
{placeholder}
|
{placeholder}
|
||||||
</Placeholder>
|
</Placeholder>
|
||||||
: null}
|
: null}
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@@ -126,7 +126,7 @@ const rules = [
|
|||||||
|
|
||||||
transform.insertNodeByKey(node.key, index, text, OPTS)
|
transform.insertNodeByKey(node.key, index, text, OPTS)
|
||||||
|
|
||||||
node.nodes.forEach(child => {
|
node.nodes.forEach((child) => {
|
||||||
transform.removeNodeByKey(child.key, OPTS)
|
transform.removeNodeByKey(child.key, OPTS)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@@ -43,7 +43,7 @@ const OPERATIONS = {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
export function applyOperation(transform, operation) {
|
export function applyOperation(transform, operation) {
|
||||||
let { state, operations } = transform
|
const { state, operations } = transform
|
||||||
const { type } = operation
|
const { type } = operation
|
||||||
const fn = OPERATIONS[type]
|
const fn = OPERATIONS[type]
|
||||||
|
|
||||||
@@ -260,7 +260,6 @@ function removeNode(state, operation) {
|
|||||||
selection = selection.unset()
|
selection = selection.unset()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove the node from the document.
|
// Remove the node from the document.
|
||||||
@@ -362,7 +361,7 @@ function setNode(state, operation) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
function setSelection(state, operation) {
|
function setSelection(state, operation) {
|
||||||
let properties = { ...operation.properties }
|
const properties = { ...operation.properties }
|
||||||
let { document, selection } = state
|
let { document, selection } = state
|
||||||
|
|
||||||
if (properties.anchorPath !== undefined) {
|
if (properties.anchorPath !== undefined) {
|
||||||
|
@@ -289,8 +289,8 @@ export function setInline(transform, properties) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
export function splitBlock(transform, depth = 1) {
|
export function splitBlock(transform, depth = 1) {
|
||||||
let { state } = transform
|
const { state } = transform
|
||||||
let { selection } = state
|
const { selection } = state
|
||||||
transform
|
transform
|
||||||
.snapshotSelection()
|
.snapshotSelection()
|
||||||
.splitBlockAtRange(selection, depth)
|
.splitBlockAtRange(selection, depth)
|
||||||
@@ -306,8 +306,8 @@ export function splitBlock(transform, depth = 1) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
export function splitInline(transform, depth = Infinity) {
|
export function splitInline(transform, depth = Infinity) {
|
||||||
let { state } = transform
|
const { state } = transform
|
||||||
let { selection } = state
|
const { selection } = state
|
||||||
transform
|
transform
|
||||||
.snapshotSelection()
|
.snapshotSelection()
|
||||||
.splitInlineAtRange(selection, depth)
|
.splitInlineAtRange(selection, depth)
|
||||||
|
@@ -94,7 +94,7 @@ export function deleteAtRange(transform, range, options = {}) {
|
|||||||
|
|
||||||
// Remove all of the middle nodes, between the splits.
|
// Remove all of the middle nodes, between the splits.
|
||||||
if (middles.size) {
|
if (middles.size) {
|
||||||
middles.forEach(child => {
|
middles.forEach((child) => {
|
||||||
transform.removeNodeByKey(child.key, OPTS)
|
transform.removeNodeByKey(child.key, OPTS)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -793,7 +793,7 @@ export function setInlineAtRange(transform, range, properties, options = {}) {
|
|||||||
const inlines = document.getInlinesAtRange(range)
|
const inlines = document.getInlinesAtRange(range)
|
||||||
|
|
||||||
inlines.forEach((inline) => {
|
inlines.forEach((inline) => {
|
||||||
transform.setNodeByKey(inline.key, properties, { normalize})
|
transform.setNodeByKey(inline.key, properties, { normalize })
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -940,7 +940,7 @@ export function unwrapBlockAtRange(transform, range, properties, options = {}) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
const firstMatch = children.first()
|
const firstMatch = children.first()
|
||||||
let lastMatch = children.last()
|
const lastMatch = children.last()
|
||||||
|
|
||||||
if (first == firstMatch && last == lastMatch) {
|
if (first == firstMatch && last == lastMatch) {
|
||||||
block.nodes.forEach((child, i) => {
|
block.nodes.forEach((child, i) => {
|
||||||
@@ -1061,23 +1061,25 @@ export function wrapBlockAtRange(transform, range, block, options = {}) {
|
|||||||
const lastblock = blocks.last()
|
const lastblock = blocks.last()
|
||||||
let parent, siblings, index
|
let parent, siblings, index
|
||||||
|
|
||||||
// if there is only one block in the selection then we know the parent and siblings
|
// If there is only one block in the selection then we know the parent and
|
||||||
|
// siblings.
|
||||||
if (blocks.length === 1) {
|
if (blocks.length === 1) {
|
||||||
parent = document.getParent(firstblock.key)
|
parent = document.getParent(firstblock.key)
|
||||||
siblings = blocks
|
siblings = blocks
|
||||||
}
|
}
|
||||||
|
|
||||||
// determine closest shared parent to all blocks in selection
|
// Determine closest shared parent to all blocks in selection.
|
||||||
else {
|
else {
|
||||||
parent = document.getClosest(firstblock.key, p1 => {
|
parent = document.getClosest(firstblock.key, (p1) => {
|
||||||
return !!document.getClosest(lastblock.key, p2 => p1 == p2)
|
return !!document.getClosest(lastblock.key, p2 => p1 == p2)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// if no shared parent could be found then the parent is the document
|
// If no shared parent could be found then the parent is the document.
|
||||||
if (parent == null) parent = document
|
if (parent == null) parent = document
|
||||||
|
|
||||||
// create a list of direct children siblings of parent that fall in the selection
|
// Create a list of direct children siblings of parent that fall in the
|
||||||
|
// selection.
|
||||||
if (siblings == null) {
|
if (siblings == null) {
|
||||||
const indexes = parent.nodes.reduce((ind, node, i) => {
|
const indexes = parent.nodes.reduce((ind, node, i) => {
|
||||||
if (node == firstblock || node.hasDescendant(firstblock.key)) ind[0] = i
|
if (node == firstblock || node.hasDescendant(firstblock.key)) ind[0] = i
|
||||||
@@ -1089,15 +1091,15 @@ export function wrapBlockAtRange(transform, range, block, options = {}) {
|
|||||||
siblings = parent.nodes.slice(indexes[0], indexes[1] + 1)
|
siblings = parent.nodes.slice(indexes[0], indexes[1] + 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the index to place the new wrapped node at
|
// Get the index to place the new wrapped node at.
|
||||||
if (index == null) {
|
if (index == null) {
|
||||||
index = parent.nodes.indexOf(siblings.first())
|
index = parent.nodes.indexOf(siblings.first())
|
||||||
}
|
}
|
||||||
|
|
||||||
// inject the new block node into the parent
|
// Inject the new block node into the parent.
|
||||||
transform.insertNodeByKey(parent.key, index, block, OPTS)
|
transform.insertNodeByKey(parent.key, index, block, OPTS)
|
||||||
|
|
||||||
// move the sibling nodes into the new block node
|
// Move the sibling nodes into the new block node.
|
||||||
siblings.forEach((node, i) => {
|
siblings.forEach((node, i) => {
|
||||||
transform.moveNodeByKey(node.key, block.key, i, OPTS)
|
transform.moveNodeByKey(node.key, block.key, i, OPTS)
|
||||||
})
|
})
|
||||||
@@ -1140,7 +1142,7 @@ export function wrapInlineAtRange(transform, range, inline, options = {}) {
|
|||||||
let startBlock = document.getClosestBlock(startKey)
|
let startBlock = document.getClosestBlock(startKey)
|
||||||
let endBlock = document.getClosestBlock(endKey)
|
let endBlock = document.getClosestBlock(endKey)
|
||||||
let startChild = startBlock.getHighestChild(startKey)
|
let startChild = startBlock.getHighestChild(startKey)
|
||||||
let endChild = endBlock.getHighestChild(endKey)
|
const endChild = endBlock.getHighestChild(endKey)
|
||||||
const startIndex = startBlock.nodes.indexOf(startChild)
|
const startIndex = startBlock.nodes.indexOf(startChild)
|
||||||
const endIndex = endBlock.nodes.indexOf(endChild)
|
const endIndex = endBlock.nodes.indexOf(endChild)
|
||||||
|
|
||||||
|
@@ -170,7 +170,7 @@ export function removeMarkByKey(transform, key, offset, length, mark, options =
|
|||||||
export function removeNodeByKey(transform, key, options = {}) {
|
export function removeNodeByKey(transform, key, options = {}) {
|
||||||
const { normalize = true } = options
|
const { normalize = true } = options
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
let { document } = state
|
const { document } = state
|
||||||
const path = document.getPath(key)
|
const path = document.getPath(key)
|
||||||
|
|
||||||
transform.removeNodeOperation(path)
|
transform.removeNodeOperation(path)
|
||||||
@@ -195,7 +195,7 @@ export function removeNodeByKey(transform, key, options = {}) {
|
|||||||
export function removeTextByKey(transform, key, offset, length, options = {}) {
|
export function removeTextByKey(transform, key, offset, length, options = {}) {
|
||||||
const { normalize = true } = options
|
const { normalize = true } = options
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
let { document } = state
|
const { document } = state
|
||||||
const path = document.getPath(key)
|
const path = document.getPath(key)
|
||||||
|
|
||||||
transform.removeTextOperation(path, offset, length)
|
transform.removeTextOperation(path, offset, length)
|
||||||
@@ -272,8 +272,8 @@ export function setNodeByKey(transform, key, properties, options = {}) {
|
|||||||
|
|
||||||
export function splitNodeByKey(transform, key, offset, options = {}) {
|
export function splitNodeByKey(transform, key, offset, options = {}) {
|
||||||
const { normalize = true } = options
|
const { normalize = true } = options
|
||||||
let { state } = transform
|
const { state } = transform
|
||||||
let { document } = state
|
const { document } = state
|
||||||
const path = document.getPath(key)
|
const path = document.getPath(key)
|
||||||
|
|
||||||
transform.splitNodeAtOffsetOperation(path, offset)
|
transform.splitNodeAtOffsetOperation(path, offset)
|
||||||
@@ -353,27 +353,26 @@ export function unwrapNodeByKey(transform, key, options = {}) {
|
|||||||
|
|
||||||
|
|
||||||
if (parent.nodes.size === 1) {
|
if (parent.nodes.size === 1) {
|
||||||
// Remove the parent
|
// Remove the parent and replace it by the node itself.
|
||||||
transform.removeNodeByKey(parent.key, { normalize: false })
|
transform.removeNodeByKey(parent.key, { normalize: false })
|
||||||
// and replace it by the node itself
|
|
||||||
transform.insertNodeByKey(parentParent.key, parentIndex, node, options)
|
transform.insertNodeByKey(parentParent.key, parentIndex, node, options)
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (isFirst) {
|
else if (isFirst) {
|
||||||
// Just move the node before its parent
|
// Just move the node before its parent.
|
||||||
transform.moveNodeByKey(key, parentParent.key, parentIndex, options)
|
transform.moveNodeByKey(key, parentParent.key, parentIndex, options)
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (isLast) {
|
else if (isLast) {
|
||||||
// Just move the node after its parent
|
// Just move the node after its parent.
|
||||||
transform.moveNodeByKey(key, parentParent.key, parentIndex + 1, options)
|
transform.moveNodeByKey(key, parentParent.key, parentIndex + 1, options)
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
const parentPath = document.getPath(parent.key)
|
const parentPath = document.getPath(parent.key)
|
||||||
// Split the parent
|
// Split the parent.
|
||||||
transform.splitNodeOperation(parentPath, index)
|
transform.splitNodeOperation(parentPath, index)
|
||||||
// Extract the node in between the splitted parent
|
// Extract the node in between the splitted parent.
|
||||||
transform.moveNodeByKey(key, parentParent.key, parentIndex + 1, { normalize: false })
|
transform.moveNodeByKey(key, parentParent.key, parentIndex + 1, { normalize: false })
|
||||||
|
|
||||||
if (normalize) {
|
if (normalize) {
|
||||||
|
@@ -132,7 +132,7 @@ function normalizeNodeAndChildren(transform, node, schema) {
|
|||||||
node = refindNode(transform, node)
|
node = refindNode(transform, node)
|
||||||
|
|
||||||
// Add any new children back onto the stack.
|
// Add any new children back onto the stack.
|
||||||
node.nodes.forEach(n => {
|
node.nodes.forEach((n) => {
|
||||||
if (set.has(n.key)) return
|
if (set.has(n.key)) return
|
||||||
stack = stack.push(n.key)
|
stack = stack.push(n.key)
|
||||||
})
|
})
|
||||||
@@ -171,7 +171,7 @@ function refindNode(transform, node) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
function normalizeNode(transform, node, schema) {
|
function normalizeNode(transform, node, schema) {
|
||||||
let max = schema.rules.length
|
const max = schema.rules.length
|
||||||
let iterations = 0
|
let iterations = 0
|
||||||
|
|
||||||
function iterate(t, n) {
|
function iterate(t, n) {
|
||||||
|
@@ -11,7 +11,7 @@ export function redo(transform) {
|
|||||||
let { undos, redos } = history
|
let { undos, redos } = history
|
||||||
|
|
||||||
// If there's no next snapshot, abort.
|
// If there's no next snapshot, abort.
|
||||||
let next = redos.peek()
|
const next = redos.peek()
|
||||||
if (!next) return
|
if (!next) return
|
||||||
|
|
||||||
// Shift the next state into the undo stack.
|
// Shift the next state into the undo stack.
|
||||||
@@ -19,7 +19,7 @@ export function redo(transform) {
|
|||||||
undos = undos.push(next)
|
undos = undos.push(next)
|
||||||
|
|
||||||
// Replay the next operations.
|
// Replay the next operations.
|
||||||
next.forEach(op => {
|
next.forEach((op) => {
|
||||||
transform.applyOperation(op)
|
transform.applyOperation(op)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -82,7 +82,7 @@ export function undo(transform) {
|
|||||||
let { undos, redos } = history
|
let { undos, redos } = history
|
||||||
|
|
||||||
// If there's no previous snapshot, abort.
|
// If there's no previous snapshot, abort.
|
||||||
let previous = undos.peek()
|
const previous = undos.peek()
|
||||||
if (!previous) return
|
if (!previous) return
|
||||||
|
|
||||||
// Shift the previous operations into the redo stack.
|
// Shift the previous operations into the redo stack.
|
||||||
@@ -90,8 +90,8 @@ export function undo(transform) {
|
|||||||
redos = redos.push(previous)
|
redos = redos.push(previous)
|
||||||
|
|
||||||
// Replay the inverse of the previous operations.
|
// Replay the inverse of the previous operations.
|
||||||
previous.slice().reverse().forEach(op => {
|
previous.slice().reverse().forEach((op) => {
|
||||||
op.inverse.forEach(inv => {
|
op.inverse.forEach((inv) => {
|
||||||
transform.applyOperation(inv)
|
transform.applyOperation(inv)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@@ -440,7 +440,7 @@ export function splitNodeAtOffsetOperation(transform, path, offset) {
|
|||||||
type: 'join_node',
|
type: 'join_node',
|
||||||
path: inversePath,
|
path: inversePath,
|
||||||
withPath: path,
|
withPath: path,
|
||||||
// we will split down to the text nodes, so we must join nodes recursively
|
// We will split down to the text nodes, so we must join nodes recursively.
|
||||||
deep: true
|
deep: true
|
||||||
}]
|
}]
|
||||||
|
|
||||||
|
@@ -20,9 +20,9 @@ const FRAGMENT_MATCHER = /data-slate-fragment="([^\s]+)"/
|
|||||||
function getTransferData(transfer) {
|
function getTransferData(transfer) {
|
||||||
let fragment = transfer.getData(TYPES.FRAGMENT) || null
|
let fragment = transfer.getData(TYPES.FRAGMENT) || null
|
||||||
let node = transfer.getData(TYPES.NODE) || null
|
let node = transfer.getData(TYPES.NODE) || null
|
||||||
let html = transfer.getData('text/html') || null
|
const html = transfer.getData('text/html') || null
|
||||||
let rich = transfer.getData('text/rtf') || null
|
const rich = transfer.getData('text/rtf') || null
|
||||||
let text = transfer.getData('text/plain') || null
|
const text = transfer.getData('text/plain') || null
|
||||||
let files
|
let files
|
||||||
|
|
||||||
// If there isn't a fragment, but there is HTML, check to see if the HTML is
|
// If there isn't a fragment, but there is HTML, check to see if the HTML is
|
||||||
|
@@ -69,7 +69,6 @@ function findKey(rawNode, rawOffset) {
|
|||||||
|
|
||||||
// Find the closest parent with an offset key attribute.
|
// Find the closest parent with an offset key attribute.
|
||||||
let closest = parentNode.closest(SELECTOR)
|
let closest = parentNode.closest(SELECTOR)
|
||||||
let offsetKey
|
|
||||||
|
|
||||||
// For void nodes, the element with the offset key will be a cousin, not an
|
// For void nodes, the element with the offset key will be a cousin, not an
|
||||||
// ancestor, so find it by going down from the nearest void parent.
|
// ancestor, so find it by going down from the nearest void parent.
|
||||||
@@ -81,7 +80,7 @@ function findKey(rawNode, rawOffset) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get the string value of the offset key attribute.
|
// Get the string value of the offset key attribute.
|
||||||
offsetKey = closest.getAttribute(ATTRIBUTE)
|
const offsetKey = closest.getAttribute(ATTRIBUTE)
|
||||||
|
|
||||||
// If we still didn't find an offset key, abort.
|
// If we still didn't find an offset key, abort.
|
||||||
if (!offsetKey) return null
|
if (!offsetKey) return null
|
||||||
|
@@ -8,7 +8,7 @@ import getWindow from 'get-window'
|
|||||||
function scrollWindow(window, cursorTop, cursorLeft, cursorHeight) {
|
function scrollWindow(window, cursorTop, cursorLeft, cursorHeight) {
|
||||||
let scrollX = window.scrollX
|
let scrollX = window.scrollX
|
||||||
let scrollY = window.scrollY
|
let scrollY = window.scrollY
|
||||||
let cursorBottom = cursorTop + cursorHeight
|
const cursorBottom = cursorTop + cursorHeight
|
||||||
|
|
||||||
if (cursorTop < 0 || cursorBottom > window.innerHeight) {
|
if (cursorTop < 0 || cursorBottom > window.innerHeight) {
|
||||||
scrollY += cursorTop - window.innerHeight / 2 + cursorHeight / 2
|
scrollY += cursorTop - window.innerHeight / 2 + cursorHeight / 2
|
||||||
@@ -22,26 +22,26 @@ function scrollWindow(window, cursorTop, cursorLeft, cursorHeight) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function scrollTo(element) {
|
function scrollTo(element) {
|
||||||
let window = getWindow(element)
|
const window = getWindow(element)
|
||||||
let s = window.getSelection()
|
const s = window.getSelection()
|
||||||
if (s.rangeCount > 0) {
|
if (s.rangeCount > 0) {
|
||||||
let selectionRect = s.getRangeAt(0).getBoundingClientRect()
|
const selectionRect = s.getRangeAt(0).getBoundingClientRect()
|
||||||
let innerRect = selectionRect
|
let innerRect = selectionRect
|
||||||
let wrapper = element
|
let wrapper = element
|
||||||
let cursorHeight = innerRect.height
|
const cursorHeight = innerRect.height
|
||||||
let cursorTop = innerRect.top
|
let cursorTop = innerRect.top
|
||||||
let cursorLeft = innerRect.left
|
let cursorLeft = innerRect.left
|
||||||
|
|
||||||
while (wrapper != window.document.body) {
|
while (wrapper != window.document.body) {
|
||||||
let wrapperRect = wrapper.getBoundingClientRect()
|
const wrapperRect = wrapper.getBoundingClientRect()
|
||||||
let currentY = cursorTop
|
const currentY = cursorTop
|
||||||
let cursorBottom = cursorTop + cursorHeight
|
const cursorBottom = cursorTop + cursorHeight
|
||||||
if (cursorTop < wrapperRect.top || cursorBottom > wrapperRect.top + wrapper.offsetHeight) {
|
if (cursorTop < wrapperRect.top || cursorBottom > wrapperRect.top + wrapper.offsetHeight) {
|
||||||
cursorTop = wrapperRect.top + wrapperRect.height / 2 - cursorHeight / 2
|
cursorTop = wrapperRect.top + wrapperRect.height / 2 - cursorHeight / 2
|
||||||
wrapper.scrollTop += currentY - cursorTop
|
wrapper.scrollTop += currentY - cursorTop
|
||||||
}
|
}
|
||||||
|
|
||||||
let currentLeft = cursorLeft
|
const currentLeft = cursorLeft
|
||||||
if (cursorLeft < wrapperRect.left || cursorLeft > wrapperRect.right) {
|
if (cursorLeft < wrapperRect.left || cursorLeft > wrapperRect.right) {
|
||||||
cursorLeft = wrapperRect.left + wrapperRect.width / 2
|
cursorLeft = wrapperRect.left + wrapperRect.width / 2
|
||||||
wrapper.scrollLeft += currentLeft - cursorLeft
|
wrapper.scrollLeft += currentLeft - cursorLeft
|
||||||
|
Reference in New Issue
Block a user