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

Enforce 2-spaces indentation

This commit is contained in:
Soreine
2016-11-10 11:03:51 +01:00
parent 094cdf8a51
commit a319499f7c
20 changed files with 328 additions and 334 deletions

View File

@@ -38,6 +38,7 @@
"import/no-named-as-default": "error",
"import/no-named-as-default-member": "error",
"import/no-unresolved": "error",
"indent": ["error", 2],
"key-spacing": ["error", { "beforeColon": false, "afterColon": true }],
"lines-around-comment": ["error", { "beforeBlockComment": true, "afterBlockComment": true }],
"new-parens": "error",

View File

@@ -50,17 +50,17 @@ class AutoMarkdown extends React.Component {
getType = (chars) => {
switch (chars) {
case '*':
case '-':
case '+': return 'list-item'
case '>': return 'block-quote'
case '#': return 'heading-one'
case '##': return 'heading-two'
case '###': return 'heading-three'
case '####': return 'heading-four'
case '#####': return 'heading-five'
case '######': return 'heading-six'
default: return null
case '*':
case '-':
case '+': return 'list-item'
case '>': return 'block-quote'
case '#': return 'heading-one'
case '##': return 'heading-two'
case '###': return 'heading-three'
case '####': return 'heading-four'
case '#####': return 'heading-five'
case '######': return 'heading-six'
default: return null
}
}
@@ -105,9 +105,9 @@ class AutoMarkdown extends React.Component {
onKeyDown = (e, data, state) => {
switch (data.key) {
case 'space': return this.onSpace(e, state)
case 'backspace': return this.onBackspace(e, state)
case 'enter': return this.onEnter(e, state)
case 'space': return this.onSpace(e, state)
case 'backspace': return this.onBackspace(e, state)
case 'enter': return this.onEnter(e, state)
}
}

View File

@@ -109,20 +109,20 @@ class RichText extends React.Component {
let mark
switch (data.key) {
case 'b':
mark = 'bold'
break
case 'i':
mark = 'italic'
break
case 'u':
mark = 'underlined'
break
case '`':
mark = 'code'
break
default:
return
case 'b':
mark = 'bold'
break
case 'i':
mark = 'italic'
break
case 'u':
mark = 'underlined'
break
case '`':
mark = 'code'
break
default:
return
}
state = state

View File

@@ -104,14 +104,14 @@ class Iframes extends React.Component {
let mark
switch (data.key) {
case 'b':
mark = 'bold'
break
case 'i':
mark = 'italic'
break
default:
return
case 'b':
mark = 'bold'
break
case 'i':
mark = 'italic'
break
default:
return
}
state = state

View File

@@ -160,8 +160,8 @@ class Images extends React.Component {
onDrop = (e, data, state, editor) => {
switch (data.type) {
case 'files': return this.onDropOrPasteFiles(e, data, state, editor)
case 'node': return this.onDropNode(e, data, state)
case 'files': return this.onDropOrPasteFiles(e, data, state, editor)
case 'node': return this.onDropNode(e, data, state)
}
}
@@ -222,8 +222,8 @@ class Images extends React.Component {
onPaste = (e, data, state, editor) => {
switch (data.type) {
case 'files': return this.onDropOrPasteFiles(e, data, state, editor)
case 'text': return this.onPasteText(e, data, state)
case 'files': return this.onDropOrPasteFiles(e, data, state, editor)
case 'text': return this.onPasteText(e, data, state)
}
}

View File

@@ -109,20 +109,20 @@ class RichText extends React.Component {
let mark
switch (data.key) {
case 'b':
mark = 'bold'
break
case 'i':
mark = 'italic'
break
case 'u':
mark = 'underlined'
break
case '`':
mark = 'code'
break
default:
return
case 'b':
mark = 'bold'
break
case 'i':
mark = 'italic'
break
case 'u':
mark = 'underlined'
break
case '`':
mark = 'code'
break
default:
return
}
state = state

View File

@@ -115,9 +115,9 @@ class Tables extends React.Component {
if (state.startBlock.type != 'table-cell') return
switch (data.key) {
case 'backspace': return this.onBackspace(e, state)
case 'delete': return this.onDelete(e, state)
case 'enter': return this.onEnter(e, state)
case 'backspace': return this.onBackspace(e, state)
case 'delete': return this.onDelete(e, state)
case 'enter': return this.onEnter(e, state)
}
}

View File

@@ -94,18 +94,18 @@ class Void extends React.Component {
if (node.kind == 'block') {
style = IS_FIREFOX
? {
pointerEvents: 'none',
width: '0px',
height: '0px',
lineHeight: '0px',
visibility: 'hidden'
}
pointerEvents: 'none',
width: '0px',
height: '0px',
lineHeight: '0px',
visibility: 'hidden'
}
: {
position: 'absolute',
top: '0px',
left: '-9999px',
textIndent: '-9999px'
}
position: 'absolute',
top: '0px',
left: '-9999px',
textIndent: '-9999px'
}
} else {
style = {
position: 'relative',

View File

@@ -226,12 +226,12 @@ function normalizeMarkComponent(render) {
if (isReactComponent(render)) return render
switch (typeOf(render)) {
case 'function':
return render
case 'object':
return props => <span style={render}>{props.children}</span>
case 'string':
return props => <span className={render}>{props.children}</span>
case 'function':
return render
case 'object':
return props => <span style={render}>{props.children}</span>
case 'string':
return props => <span className={render}>{props.children}</span>
}
}

View File

@@ -594,9 +594,9 @@ class Selection extends new Record(DEFAULTS) {
*/
moveEndOffset(n = 1) {
return this.isBackward
? this.merge({ anchorOffset: this.anchorOffset + n })
: this.merge({ focusOffset: this.focusOffset + n })
return this.isBackward
? this.merge({ anchorOffset: this.anchorOffset + n })
: this.merge({ focusOffset: this.focusOffset + n })
}
/**

View File

@@ -425,8 +425,8 @@ class State extends new Record(DEFAULTS) {
transform(properties = {}) {
const state = this
return new Transform({
...properties,
state
...properties,
state
})
}

View File

@@ -44,9 +44,9 @@ class Text extends new Record(DEFAULTS) {
*/
static createFromString(text, marks = Set()) {
return Text.createFromRanges([
Range.create({ text, marks })
])
return Text.createFromRanges([
Range.create({ text, marks })
])
}
/**
@@ -57,12 +57,12 @@ class Text extends new Record(DEFAULTS) {
*/
static createFromRanges(ranges) {
return Text.create({
characters: ranges.reduce((characters, range) => {
range = Range.create(range)
return characters.concat(range.getCharacters())
}, Character.createList())
})
return Text.create({
characters: ranges.reduce((characters, range) => {
range = Range.create(range)
return characters.concat(range.getCharacters())
}, Character.createList())
})
}
/**

View File

@@ -261,11 +261,11 @@ function Plugin(options = {}) {
debug('onDrop', { data })
switch (data.type) {
case 'text':
case 'html':
return onDropText(e, data, state)
case 'fragment':
return onDropFragment(e, data, state)
case 'text':
case 'html':
return onDropText(e, data, state)
case 'fragment':
return onDropFragment(e, data, state)
}
}
@@ -346,14 +346,14 @@ function Plugin(options = {}) {
debug('onKeyDown', { data })
switch (data.key) {
case 'enter': return onKeyDownEnter(e, data, state)
case 'backspace': return onKeyDownBackspace(e, data, state)
case 'delete': return onKeyDownDelete(e, data, state)
case 'left': return onKeyDownLeft(e, data, state)
case 'right': return onKeyDownRight(e, data, state)
case 'y': return onKeyDownY(e, data, state)
case 'z': return onKeyDownZ(e, data, state)
case 'k': return onKeyDownK(e, data, state)
case 'enter': return onKeyDownEnter(e, data, state)
case 'backspace': return onKeyDownBackspace(e, data, state)
case 'delete': return onKeyDownDelete(e, data, state)
case 'left': return onKeyDownLeft(e, data, state)
case 'right': return onKeyDownRight(e, data, state)
case 'y': return onKeyDownY(e, data, state)
case 'z': return onKeyDownZ(e, data, state)
case 'k': return onKeyDownK(e, data, state)
}
}
@@ -625,11 +625,11 @@ function Plugin(options = {}) {
debug('onPaste', { data })
switch (data.type) {
case 'fragment':
return onPasteFragment(e, data, state)
case 'text':
case 'html':
return onPasteText(e, data, state)
case 'fragment':
return onPasteFragment(e, data, state)
case 'text':
case 'html':
return onPasteText(e, data, state)
}
}
@@ -701,60 +701,60 @@ function Plugin(options = {}) {
* Extend core schema with rendering
*/
/**
* A default schema rule to render block nodes.
*
* @type {Object}
*/
/**
* A default schema rule to render block nodes.
*
* @type {Object}
*/
const BLOCK_RENDER_RULE = {
match: (node) => {
return node.kind == 'block'
},
render: (props) => {
return (
<div {...props.attributes} style={{ position: 'relative' }}>
{props.children}
{placeholder
? <Placeholder
className={placeholderClassName}
node={props.node}
parent={props.state.document}
state={props.state}
style={placeholderStyle}
>
{placeholder}
</Placeholder>
: null}
</div>
)
}
}
const BLOCK_RENDER_RULE = {
match: (node) => {
return node.kind == 'block'
},
render: (props) => {
return (
<div {...props.attributes} style={{ position: 'relative' }}>
{props.children}
{placeholder
? <Placeholder
className={placeholderClassName}
node={props.node}
parent={props.state.document}
state={props.state}
style={placeholderStyle}
>
{placeholder}
</Placeholder>
: null}
</div>
)
}
}
/**
* A default schema rule to render inline nodes.
*
* @type {Object}
*/
/**
* A default schema rule to render inline nodes.
*
* @type {Object}
*/
const INLINE_RENDER_RULE = {
match: (node) => {
return node.kind == 'inline'
},
render: (props) => {
return (
<span {...props.attributes} style={{ position: 'relative' }}>
{props.children}
</span>
)
}
}
const INLINE_RENDER_RULE = {
match: (node) => {
return node.kind == 'inline'
},
render: (props) => {
return (
<span {...props.attributes} style={{ position: 'relative' }}>
{props.children}
</span>
)
}
}
const schema = {
rules: [
BLOCK_RENDER_RULE,
INLINE_RENDER_RULE
]
rules: [
BLOCK_RENDER_RULE,
INLINE_RENDER_RULE
]
}
/**

View File

@@ -57,16 +57,16 @@ const BLOCK_CHILDREN_RULE = {
*/
const MIN_TEXT_RULE = {
match: (object) => {
return object.kind == 'block' || object.kind == 'inline'
},
validate: (node) => {
const { nodes } = node
return nodes.size === 0 ? true : null
},
normalize: (transform, node) => {
return transform.insertNodeByKey(node.key, 0, Text.create(), { normalize: false })
}
match: (object) => {
return object.kind == 'block' || object.kind == 'inline'
},
validate: (node) => {
const { nodes } = node
return nodes.size === 0 ? true : null
},
normalize: (transform, node) => {
return transform.insertNodeByKey(node.key, 0, Text.create(), { normalize: false })
}
}
/**
@@ -83,7 +83,7 @@ const INLINE_CHILDREN_RULE = {
const { nodes } = inline
const invalids = nodes.filter(n => n.kind != 'inline' && n.kind != 'text')
return invalids.size ? invalids : null
},
},
normalize: (transform, inline, invalids) => {
return invalids.reduce((t, n) => t.removeNodeByKey(n.key, { normalize: false }), transform)
}
@@ -131,11 +131,11 @@ const INLINE_VOID_TEXT_RULE = {
return node.text !== ' ' || node.nodes.size !== 1
},
normalize: (transform, node, result) => {
transform = node.nodes.reduce((t, child) => {
return t.removeNodeByKey(child.key, { normalize: false })
}, transform)
transform = node.nodes.reduce((t, child) => {
return t.removeNodeByKey(child.key, { normalize: false })
}, transform)
return transform.insertNodeByKey(node.key, 0, Text.createFromString(' '), { normalize: false })
return transform.insertNodeByKey(node.key, 0, Text.createFromString(' '), { normalize: false })
}
}
@@ -240,43 +240,43 @@ const NO_EMPTY_TEXT_RULE = {
const { nodes } = node
if (nodes.size <= 1) {
return
return
}
const invalids = nodes
.filter((desc, i) => {
if (desc.kind != 'text' || desc.length > 0) {
return
}
.filter((desc, i) => {
if (desc.kind != 'text' || desc.length > 0) {
return
}
// Empty text nodes are only allowed near inline void node
const next = nodes.get(i + 1)
const prev = i > 0 ? nodes.get(i - 1) : null
// Empty text nodes are only allowed near inline void node
const next = nodes.get(i + 1)
const prev = i > 0 ? nodes.get(i - 1) : null
// If last one and previous is an inline void, we need to preserve it
if (!next && isInlineVoid(prev)) {
return
}
// If last one and previous is an inline void, we need to preserve it
if (!next && isInlineVoid(prev)) {
return
}
// If first one and next one is an inline, we preserve it
if (!prev && isInlineVoid(next)) {
return
}
// If first one and next one is an inline, we preserve it
if (!prev && isInlineVoid(next)) {
return
}
// If surrounded by inline void, we preserve it
if (next && prev && isInlineVoid(next) && isInlineVoid(prev)) {
return
}
// If surrounded by inline void, we preserve it
if (next && prev && isInlineVoid(next) && isInlineVoid(prev)) {
return
}
// Otherwise we remove it
return true
})
// Otherwise we remove it
return true
})
return invalids.size ? invalids : null
},
normalize: (transform, node, invalids) => {
return invalids.reduce((t, text) => {
return t.removeNodeByKey(text.key, { normalize: false })
return t.removeNodeByKey(text.key, { normalize: false })
}, transform)
}
}

View File

@@ -129,17 +129,17 @@ class Html {
elements.forEach((element) => {
const node = this.deserializeElement(element)
switch (typeOf(node)) {
case 'array':
nodes = nodes.concat(node)
break
case 'object':
nodes.push(node)
break
case 'null':
case 'undefined':
return
default:
throw new Error(`A rule returned an invalid deserialized representation: "${node}".`)
case 'array':
nodes = nodes.concat(node)
break
case 'object':
nodes.push(node)
break
case 'null':
case 'undefined':
return
default:
throw new Error(`A rule returned an invalid deserialized representation: "${node}".`)
}
})
@@ -158,15 +158,15 @@ class Html {
const next = (elements) => {
switch (typeOf(elements)) {
case 'array':
return this.deserializeElements(elements)
case 'object':
return this.deserializeElement(elements)
case 'null':
case 'undefined':
return
default:
throw new Error(`The \`next\` argument was called with invalid children: "${elements}".`)
case 'array':
return this.deserializeElements(elements)
case 'object':
return this.deserializeElement(elements)
case 'null':
case 'undefined':
return
default:
throw new Error(`The \`next\` argument was called with invalid children: "${elements}".`)
}
}

View File

@@ -110,13 +110,13 @@ const Raw = {
deserializeNode(object, options) {
switch (object.kind) {
case 'block': return Raw.deserializeBlock(object, options)
case 'document': return Raw.deserializeDocument(object, options)
case 'inline': return Raw.deserializeInline(object, options)
case 'text': return Raw.deserializeText(object, options)
default: {
throw new Error(`Unrecognized node kind "${object.kind}".`)
}
case 'block': return Raw.deserializeBlock(object, options)
case 'document': return Raw.deserializeDocument(object, options)
case 'inline': return Raw.deserializeInline(object, options)
case 'text': return Raw.deserializeText(object, options)
default: {
throw new Error(`Unrecognized node kind "${object.kind}".`)
}
}
},
@@ -291,13 +291,13 @@ const Raw = {
serializeNode(node, options) {
switch (node.kind) {
case 'block': return Raw.serializeBlock(node, options)
case 'document': return Raw.serializeDocument(node, options)
case 'inline': return Raw.serializeInline(node, options)
case 'text': return Raw.serializeText(node, options)
default: {
throw new Error(`Unrecognized node kind "${node.kind}".`)
}
case 'block': return Raw.serializeBlock(node, options)
case 'document': return Raw.serializeDocument(node, options)
case 'inline': return Raw.serializeInline(node, options)
case 'text': return Raw.serializeText(node, options)
default: {
throw new Error(`Unrecognized node kind "${node.kind}".`)
}
}
},

View File

@@ -193,10 +193,10 @@ export function normalizeWith(transform, schema, prevDocument) {
*/
export function normalize(transform) {
transform = transform
.normalizeDocument()
.normalizeSelection()
return transform
transform = transform
.normalizeDocument()
.normalizeSelection()
return transform
}
/**
@@ -272,15 +272,15 @@ export function normalizeSelection(transform) {
!document.hasDescendant(selection.anchorKey) ||
!document.hasDescendant(selection.focusKey)
) {
warning('Selection was invalid and reset to start of the document')
const firstText = document.getTexts().first()
selection = selection.merge({
anchorKey: firstText.key,
anchorOffset: 0,
focusKey: firstText.key,
focusOffset: 0,
isBackward: false
})
warning('Selection was invalid and reset to start of the document')
const firstText = document.getTexts().first()
selection = selection.merge({
anchorKey: firstText.key,
anchorOffset: 0,
focusKey: firstText.key,
focusOffset: 0,
isBackward: false
})
}
state = state.merge({ selection })

View File

@@ -380,10 +380,10 @@ export function setSelectionOperation(transform, properties) {
// If the selection moves, clear any marks, unless the new selection
// does change the marks in some way
const moved = [
'anchorKey',
'anchorOffset',
'focusKey',
'focusOffset',
'anchorKey',
'anchorOffset',
'focusKey',
'focusOffset',
].some(p => props.hasOwnProperty(p))
if (selection.marks

View File

@@ -20,13 +20,12 @@ function block(value) {
if (value instanceof Block) return value
switch (typeOf(value)) {
case 'string':
case 'object': {
return Block.create(nodeProperties(value))
}
default: {
throw new Error(`Invalid \`block\` argument! It must be a block, an object, or a string. You passed: "${value}".`)
}
case 'string':
case 'object':
return Block.create(nodeProperties(value))
default:
throw new Error(`Invalid \`block\` argument! It must be a block, an object, or a string. You passed: "${value}".`)
}
}
@@ -41,13 +40,12 @@ function inline(value) {
if (value instanceof Inline) return value
switch (typeOf(value)) {
case 'string':
case 'object': {
return Inline.create(nodeProperties(value))
}
default: {
throw new Error(`Invalid \`inline\` argument! It must be an inline, an object, or a string. You passed: "${value}".`)
}
case 'string':
case 'object':
return Inline.create(nodeProperties(value))
default:
throw new Error(`Invalid \`inline\` argument! It must be an inline, an object, or a string. You passed: "${value}".`)
}
}
@@ -81,13 +79,12 @@ function mark(value) {
if (value instanceof Mark) return value
switch (typeOf(value)) {
case 'string':
case 'object': {
return Mark.create(markProperties(value))
}
default: {
throw new Error(`Invalid \`mark\` argument! It must be a mark, an object, or a string. You passed: "${value}".`)
}
case 'string':
case 'object':
return Mark.create(markProperties(value))
default:
throw new Error(`Invalid \`mark\` argument! It must be a mark, an object, or a string. You passed: "${value}".`)
}
}
@@ -102,23 +99,22 @@ function markProperties(value = {}) {
const ret = {}
switch (typeOf(value)) {
case 'string': {
ret.type = value
break
}
case 'object': {
for (const k in value) {
if (k == 'data') {
if (value[k] !== undefined) ret[k] = Data.create(value[k])
} else {
ret[k] = value[k]
}
case 'string':
ret.type = value
break
case 'object':
for (const k in value) {
if (k == 'data') {
if (value[k] !== undefined) ret[k] = Data.create(value[k])
} else {
ret[k] = value[k]
}
break
}
default: {
throw new Error(`Invalid mark \`properties\` argument! It must be an object, a string or a mark. You passed: "${value}".`)
}
break
default:
throw new Error(`Invalid mark \`properties\` argument! It must be an object, a string or a mark. You passed: "${value}".`)
}
return ret
@@ -135,24 +131,23 @@ function nodeProperties(value = {}) {
const ret = {}
switch (typeOf(value)) {
case 'string': {
ret.type = value
break
}
case 'object': {
if (value.isVoid !== undefined) ret.isVoid = !!value.isVoid
for (const k in value) {
if (k == 'data') {
if (value[k] !== undefined) ret[k] = Data.create(value[k])
} else {
ret[k] = value[k]
}
case 'string':
ret.type = value
break
case 'object':
if (value.isVoid !== undefined) ret.isVoid = !!value.isVoid
for (const k in value) {
if (k == 'data') {
if (value[k] !== undefined) ret[k] = Data.create(value[k])
} else {
ret[k] = value[k]
}
break
}
default: {
throw new Error(`Invalid node \`properties\` argument! It must be an object, a string or a node. You passed: "${value}".`)
}
break
default:
throw new Error(`Invalid node \`properties\` argument! It must be an object, a string or a node. You passed: "${value}".`)
}
return ret
@@ -169,12 +164,11 @@ function selection(value) {
if (value instanceof Selection) return value
switch (typeOf(value)) {
case 'object': {
return Selection.create(value)
}
default: {
throw new Error(`Invalid \`selection\` argument! It must be a selection or an object. You passed: "${value}".`)
}
case 'object':
return Selection.create(value)
default:
throw new Error(`Invalid \`selection\` argument! It must be a selection or an object. You passed: "${value}".`)
}
}
@@ -189,19 +183,18 @@ function selectionProperties(value = {}) {
const ret = {}
switch (typeOf(value)) {
case 'object': {
if (value.anchorKey !== undefined) ret.anchorKey = value.anchorKey
if (value.anchorOffset !== undefined) ret.anchorOffset = value.anchorOffset
if (value.focusKey !== undefined) ret.focusKey = value.focusKey
if (value.focusOffset !== undefined) ret.focusOffset = value.focusOffset
if (value.isBackward !== undefined) ret.isBackward = !!value.isBackward
if (value.isFocused !== undefined) ret.isFocused = !!value.isFocused
if (value.marks !== undefined) ret.marks = value.marks
break
}
default: {
throw new Error(`Invalid selection \`properties\` argument! It must be an object or a selection. You passed: "${value}".`)
}
case 'object':
if (value.anchorKey !== undefined) ret.anchorKey = value.anchorKey
if (value.anchorOffset !== undefined) ret.anchorOffset = value.anchorOffset
if (value.focusKey !== undefined) ret.focusKey = value.focusKey
if (value.focusOffset !== undefined) ret.focusOffset = value.focusOffset
if (value.isBackward !== undefined) ret.isBackward = !!value.isBackward
if (value.isFocused !== undefined) ret.isFocused = !!value.isFocused
if (value.marks !== undefined) ret.marks = value.marks
break
default:
throw new Error(`Invalid selection \`properties\` argument! It must be an object or a selection. You passed: "${value}".`)
}
return ret

View File

@@ -41,22 +41,22 @@ class Transfer {
data.type = type
switch (type) {
case 'files':
data.files = this.getFiles()
break
case 'fragment':
data.fragment = this.getFragment()
break
case 'html':
data.html = this.getHtml()
data.text = this.getText()
break
case 'node':
data.node = this.getNode()
break
case 'text':
data.text = this.getText()
break
case 'files':
data.files = this.getFiles()
break
case 'fragment':
data.fragment = this.getFragment()
break
case 'html':
data.html = this.getHtml()
data.text = this.getText()
break
case 'node':
data.node = this.getNode()
break
case 'text':
data.text = this.getText()
break
}
return data
@@ -76,8 +76,8 @@ class Transfer {
if (data.items && data.items.length) {
const fileItems = Array.from(data.items)
.map(item => item.kind == 'file' ? item.getAsFile() : null)
.filter(exists => exists)
.map(item => item.kind == 'file' ? item.getAsFile() : null)
.filter(exists => exists)
if (fileItems.length) files = fileItems
}