1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-24 07:52:50 +02:00

Fixing onDrop for Inline Nodes, Harder type checking in normalize fun… (#1033)

* Fixing onDrop for Inline Nodes, Harder type checking in normalize functions, Omitting Pseudo-symboles from nodeProperties/markProperties

* Refactoring code

* Making normalize blacklist clearer

* Update normalize.js

* Update core.js

* Linting fixes, no trailing spaces
This commit is contained in:
Ryan Yurkanin
2017-09-05 12:59:35 -04:00
committed by Ian Storm Taylor
parent 43dbfbcb58
commit aec3b702c4
2 changed files with 61 additions and 14 deletions

View File

@@ -1,7 +1,9 @@
import Base64 from '../serializers/base-64' import Base64 from '../serializers/base-64'
import Content from '../components/content' import Content from '../components/content'
import Block from '../models/block'
import Character from '../models/character' import Character from '../models/character'
import Inline from '../models/inline'
import Debug from 'debug' import Debug from 'debug'
import getPoint from '../utils/get-point' import getPoint from '../utils/get-point'
import Placeholder from '../components/placeholder' import Placeholder from '../components/placeholder'
@@ -381,12 +383,21 @@ function Plugin(options = {}) {
if (isInternal) transform.delete() if (isInternal) transform.delete()
if (Block.isBlock(node)) {
return transform return transform
.select(target) .select(target)
.insertBlock(node) .insertBlock(node)
.removeNodeByKey(node.key) .removeNodeByKey(node.key)
.apply() .apply()
} }
if (Inline.isInline(node)) {
return transform
.select(target)
.insertInline(node)
.removeNodeByKey(node.key)
.apply()
}
}
/** /**
* On drop fragment. * On drop fragment.

View File

@@ -18,12 +18,20 @@ import typeOf from 'type-of'
function block(value) { function block(value) {
if (Block.isBlock(value)) return value if (Block.isBlock(value)) return value
if (
Inline.isInline(value) ||
Mark.isMark(value) ||
Text.isText(value) ||
Selection.isSelection(value)
) {
throw new Error(`Invalid \`block\` argument! It must be a block, an object, or a string. You passed: "${value}".`)
}
switch (typeOf(value)) { switch (typeOf(value)) {
case 'string': case 'string':
case 'object': case 'object':
return Block.create(nodeProperties(value)) return Block.create(nodeProperties(value))
default: default:
throw new Error(`Invalid \`block\` argument! It must be a block, an object, or a string. You passed: "${value}".`) throw new Error(`Invalid \`block\` argument! It must be a block, an object, or a string. You passed: "${value}".`)
} }
@@ -38,12 +46,19 @@ function block(value) {
function inline(value) { function inline(value) {
if (Inline.isInline(value)) return value if (Inline.isInline(value)) return value
if (
Block.isBlock(value) ||
Mark.isMark(value) ||
Text.isText(value) ||
Selection.isSelection(value)
) {
throw new Error(`Invalid \`inline\` argument! It must be an inline, an object, or a string. You passed: "${value}".`)
}
switch (typeOf(value)) { switch (typeOf(value)) {
case 'string': case 'string':
case 'object': case 'object':
return Inline.create(nodeProperties(value)) return Inline.create(nodeProperties(value))
default: default:
throw new Error(`Invalid \`inline\` argument! It must be an inline, an object, or a string. You passed: "${value}".`) throw new Error(`Invalid \`inline\` argument! It must be an inline, an object, or a string. You passed: "${value}".`)
} }
@@ -60,10 +75,14 @@ function key(value) {
if (typeOf(value) == 'string') return value if (typeOf(value) == 'string') return value
warn('An object was passed to a Node method instead of a `key` string. This was previously supported, but is being deprecated because it can have a negative impact on performance. The object in question was:', value) warn('An object was passed to a Node method instead of a `key` string. This was previously supported, but is being deprecated because it can have a negative impact on performance. The object in question was:', value)
if (Block.isBlock(value)) return value.key if (
if (Document.isDocument(value)) return value.key Block.isBlock(value) ||
if (Inline.isInline(value)) return value.key Document.isDocument(value) ||
if (Text.isText(value)) return value.key Inline.isInline(value) ||
Text.isText(value)
) {
return value.key
}
throw new Error(`Invalid \`key\` argument! It must be either a block, an inline, a text, or a string. You passed: "${value}".`) throw new Error(`Invalid \`key\` argument! It must be either a block, an inline, a text, or a string. You passed: "${value}".`)
} }
@@ -77,12 +96,19 @@ function key(value) {
function mark(value) { function mark(value) {
if (Mark.isMark(value)) return value if (Mark.isMark(value)) return value
if (
Block.isBlock(value) ||
Inline.isInline(value) ||
Text.isText(value) ||
Selection.isSelection(value)
) {
throw new Error(`Invalid \`mark\` argument! It must be a mark, an object, or a string. You passed: "${value}".`)
}
switch (typeOf(value)) { switch (typeOf(value)) {
case 'string': case 'string':
case 'object': case 'object':
return Mark.create(markProperties(value)) return Mark.create(markProperties(value))
default: default:
throw new Error(`Invalid \`mark\` argument! It must be a mark, an object, or a string. You passed: "${value}".`) throw new Error(`Invalid \`mark\` argument! It must be a mark, an object, or a string. You passed: "${value}".`)
} }
@@ -107,6 +133,8 @@ function markProperties(value = {}) {
for (const k in value) { for (const k in value) {
if (k == 'data') { if (k == 'data') {
if (value[k] !== undefined) ret[k] = Data.create(value[k]) if (value[k] !== undefined) ret[k] = Data.create(value[k])
} else if (k.startsWith('@@__SLATE')) {
return
} else { } else {
ret[k] = value[k] ret[k] = value[k]
} }
@@ -140,6 +168,8 @@ function nodeProperties(value = {}) {
for (const k in value) { for (const k in value) {
if (k == 'data') { if (k == 'data') {
if (value[k] !== undefined) ret[k] = Data.create(value[k]) if (value[k] !== undefined) ret[k] = Data.create(value[k])
} else if (k.startsWith('@@__SLATE')) {
return
} else { } else {
ret[k] = value[k] ret[k] = value[k]
} }
@@ -162,11 +192,18 @@ function nodeProperties(value = {}) {
function selection(value) { function selection(value) {
if (Selection.isSelection(value)) return value if (Selection.isSelection(value)) return value
if (
Mark.isMark(value) ||
Block.isBlock(value) ||
Inline.isInline(value) ||
Text.isText(value)
) {
throw new Error(`Invalid \`selection\` argument! It must be a selection or an object. You passed: "${value}".`)``
}
switch (typeOf(value)) { switch (typeOf(value)) {
case 'object': case 'object':
return Selection.create(value) return Selection.create(value)
default: default:
throw new Error(`Invalid \`selection\` argument! It must be a selection or an object. You passed: "${value}".`) throw new Error(`Invalid \`selection\` argument! It must be a selection or an object. You passed: "${value}".`)
} }
@@ -192,7 +229,6 @@ function selectionProperties(value = {}) {
if (value.isFocused !== undefined) ret.isFocused = !!value.isFocused if (value.isFocused !== undefined) ret.isFocused = !!value.isFocused
if (value.marks !== undefined) ret.marks = value.marks if (value.marks !== undefined) ret.marks = value.marks
break break
default: default:
throw new Error(`Invalid selection \`properties\` argument! It must be an object or a selection. You passed: "${value}".`) throw new Error(`Invalid selection \`properties\` argument! It must be an object or a selection. You passed: "${value}".`)
} }