1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-30 18:39:51 +02:00

add insertFragment transform tests

This commit is contained in:
Ian Storm Taylor
2016-07-21 14:57:17 -07:00
parent 45d671dba0
commit 7615ce8611
40 changed files with 738 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
nodes:
- kind: block
type: list-item
nodes:
- kind: text
ranges:
- text: fragment

View File

@@ -0,0 +1,40 @@
import assert from 'assert'
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).document
const { document, selection } = state
const texts = document.getTexts()
const first = texts.first()
const last = fragment.getTexts().last()
const range = selection.merge({
anchorKey: first.key,
anchorOffset: first.length,
focusKey: first.key,
focusOffset: first.length
})
const next = state
.transform()
.moveTo(range)
.insertFragment(fragment)
.apply()
assert.deepEqual(
next.selection.toJS(),
range.merge({
anchorKey: first.key,
anchorOffset: first.length + last.length,
focusKey: first.key,
focusOffset: first.length + last.length
}).toJS()
)
return next
}

View File

@@ -0,0 +1,8 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: word

View File

@@ -0,0 +1,8 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: wordfragment

View File

@@ -0,0 +1,8 @@
nodes:
- kind: block
type: list-item
nodes:
- kind: text
ranges:
- text: fragment

View File

@@ -0,0 +1,41 @@
import assert from 'assert'
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).document
const { document, selection } = state
const texts = document.getTexts()
const first = texts.first()
const range = selection.merge({
anchorKey: first.key,
anchorOffset: first.length,
focusKey: first.key,
focusOffset: first.length
})
const next = state
.transform()
.moveTo(range)
.insertFragment(fragment)
.apply()
const last = next.document.getTexts().last()
assert.deepEqual(
next.selection.toJS(),
range.merge({
anchorKey: last.key,
anchorOffset: last.length,
focusKey: last.key,
focusOffset: last.length
}).toJS()
)
return next
}

View File

@@ -0,0 +1,11 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: inline
type: link
nodes:
- kind: text
ranges:
- text: word

View File

@@ -0,0 +1,14 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: inline
type: link
nodes:
- kind: text
ranges:
- text: word
- kind: text
ranges:
- text: fragment

View File

@@ -0,0 +1,14 @@
nodes:
- kind: block
type: list-item
nodes:
- kind: text
ranges:
- text: fragment one
- kind: block
type: list-item
nodes:
- kind: text
ranges:
- text: fragment two

View File

@@ -0,0 +1,42 @@
import assert from 'assert'
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).document
const { document, selection } = state
const texts = document.getTexts()
const first = texts.first()
const last = fragment.getTexts().last()
const range = selection.merge({
anchorKey: first.key,
anchorOffset: 2,
focusKey: first.key,
focusOffset: 2
})
const next = state
.transform()
.moveTo(range)
.insertFragment(fragment)
.apply()
const updated = next.document.getTexts().last()
assert.deepEqual(
next.selection.toJS(),
range.merge({
anchorKey: updated.key,
anchorOffset: last.length,
focusKey: updated.key,
focusOffset: last.length
}).toJS()
)
return next
}

View File

@@ -0,0 +1,8 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: word

View File

@@ -0,0 +1,14 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: wofragment one
- kind: block
type: list-item
nodes:
- kind: text
ranges:
- text: fragment tword

View File

@@ -0,0 +1,8 @@
nodes:
- kind: block
type: list-item
nodes:
- kind: text
ranges:
- text: fragment

View File

@@ -0,0 +1,40 @@
import assert from 'assert'
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).document
const { document, selection } = state
const texts = document.getTexts()
const first = texts.first()
const last = fragment.getTexts().last()
const range = selection.merge({
anchorKey: first.key,
anchorOffset: 2,
focusKey: first.key,
focusOffset: 2
})
const next = state
.transform()
.moveTo(range)
.insertFragment(fragment)
.apply()
assert.deepEqual(
next.selection.toJS(),
range.merge({
anchorKey: first.key,
anchorOffset: range.anchorOffset + last.length,
focusKey: first.key,
focusOffset: range.focusOffset + last.length
}).toJS()
)
return next
}

View File

@@ -0,0 +1,8 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: word

View File

@@ -0,0 +1,8 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: wofragmentrd

View File

@@ -0,0 +1,11 @@
nodes:
- kind: block
type: list-item
nodes:
- kind: inline
type: hashtag
nodes:
- kind: text
ranges:
- text: fragment

View File

@@ -0,0 +1,42 @@
import assert from 'assert'
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).document
const { document, selection } = state
const texts = document.getTexts()
const first = texts.first()
const last = fragment.getTexts().last()
const range = selection.merge({
anchorKey: first.key,
anchorOffset: 2,
focusKey: first.key,
focusOffset: 2
})
const next = state
.transform()
.moveTo(range)
.insertFragment(fragment)
.apply()
const updated = next.document.getTexts().get(1)
// assert.deepEqual(
// next.selection.toJS(),
// range.merge({
// anchorKey: updated.key,
// anchorOffset: last.length,
// focusKey: updated.key,
// focusOffset: last.length
// }).toJS()
// )
return next
}

View File

@@ -0,0 +1,11 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: inline
type: link
nodes:
- kind: text
ranges:
- text: word

View File

@@ -0,0 +1,23 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: inline
type: link
nodes:
- kind: text
ranges:
- text: wo
- kind: inline
type: hashtag
nodes:
- kind: text
ranges:
- text: fragment
- kind: inline
type: link
nodes:
- kind: text
ranges:
- text: rd

View File

@@ -0,0 +1,8 @@
nodes:
- kind: block
type: list-item
nodes:
- kind: text
ranges:
- text: fragment

View File

@@ -0,0 +1,42 @@
import assert from 'assert'
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).document
const { document, selection } = state
const texts = document.getTexts()
const first = texts.first()
const last = fragment.getTexts().last()
const range = selection.merge({
anchorKey: first.key,
anchorOffset: 2,
focusKey: first.key,
focusOffset: 2
})
const next = state
.transform()
.moveTo(range)
.insertFragment(fragment)
.apply()
const updated = next.document.getTexts().last()
assert.deepEqual(
next.selection.toJS(),
range.merge({
anchorKey: updated.key,
anchorOffset: last.length,
focusKey: updated.key,
focusOffset: last.length
}).toJS()
)
return next
}

View File

@@ -0,0 +1,11 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: inline
type: link
nodes:
- kind: text
ranges:
- text: word

View File

@@ -0,0 +1,20 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: inline
type: link
nodes:
- kind: text
ranges:
- text: wo
- kind: text
ranges:
- text: fragment
- kind: inline
type: link
nodes:
- kind: text
ranges:
- text: rd

View File

@@ -0,0 +1,8 @@
nodes:
- kind: block
type: list-item
nodes:
- kind: text
ranges:
- text: fragment

View File

@@ -0,0 +1,42 @@
import assert from 'assert'
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).document
const { document, selection } = state
const texts = document.getTexts()
const first = texts.first()
const last = fragment.getTexts().last()
const range = selection.merge({
anchorKey: first.key,
anchorOffset: 0,
focusKey: first.key,
focusOffset: 0
})
const next = state
.transform()
.moveTo(range)
.insertFragment(fragment)
.apply()
const updated = next.document.getTexts().first()
assert.deepEqual(
next.selection.toJS(),
range.merge({
anchorKey: updated.key,
anchorOffset: last.length,
focusKey: updated.key,
focusOffset: last.length
}).toJS()
)
return next
}

View File

@@ -0,0 +1,8 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: word

View File

@@ -0,0 +1,8 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: fragmentword

View File

@@ -0,0 +1,8 @@
nodes:
- kind: block
type: list-item
nodes:
- kind: text
ranges:
- text: fragment

View File

@@ -0,0 +1,42 @@
import assert from 'assert'
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).document
const { document, selection } = state
const texts = document.getTexts()
const first = texts.first()
const last = fragment.getTexts().last()
const range = selection.merge({
anchorKey: first.key,
anchorOffset: 0,
focusKey: first.key,
focusOffset: 0
})
const next = state
.transform()
.moveTo(range)
.insertFragment(fragment)
.apply()
const updated = next.document.getTexts().first()
assert.deepEqual(
next.selection.toJS(),
range.merge({
anchorKey: updated.key,
anchorOffset: last.length,
focusKey: updated.key,
focusOffset: last.length
}).toJS()
)
return next
}

View File

@@ -0,0 +1,11 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: inline
type: link
nodes:
- kind: text
ranges:
- text: word

View File

@@ -0,0 +1,14 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: fragment
- kind: inline
type: link
nodes:
- kind: text
ranges:
- text: word

View File

@@ -0,0 +1,8 @@
nodes:
- kind: block
type: list-item
nodes:
- kind: text
ranges:
- text: fragment

View File

@@ -0,0 +1,42 @@
import assert from 'assert'
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).document
const { document, selection } = state
const texts = document.getTexts()
const second = texts.get(1)
const last = fragment.getTexts().last()
const range = selection.merge({
anchorKey: second.key,
anchorOffset: 0,
focusKey: second.key,
focusOffset: 0
})
const next = state
.transform()
.moveTo(range)
.insertFragment(fragment)
.apply()
const updated = next.document.getTexts().get(1)
assert.deepEqual(
next.selection.toJS(),
range.merge({
anchorKey: updated.key,
anchorOffset: last.length,
focusKey: updated.key,
focusOffset: last.length
}).toJS()
)
return next
}

View File

@@ -0,0 +1,14 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: word
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: another

View File

@@ -0,0 +1,14 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: word
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: fragmentanother

View File

@@ -0,0 +1,8 @@
nodes:
- kind: block
type: list-item
nodes:
- kind: text
ranges:
- text: fragment

View File

@@ -0,0 +1,41 @@
import assert from 'assert'
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).document
const { document, selection } = state
const texts = document.getTexts()
const first = texts.first()
const last = texts.last()
const fragLast = fragment.getTexts().last()
const range = selection.merge({
anchorKey: first.key,
anchorOffset: 2,
focusKey: last.key,
focusOffset: 2
})
const next = state
.transform()
.moveTo(range)
.insertFragment(fragment)
.apply()
assert.deepEqual(
next.selection.toJS(),
range.merge({
anchorKey: first.key,
anchorOffset: range.anchorOffset + fragLast.length,
focusKey: first.key,
focusOffset: range.focusOffset + fragLast.length
}).toJS()
)
return next
}

View File

@@ -0,0 +1,14 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: word
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: another

View File

@@ -0,0 +1,8 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: wofragmentother