mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-22 15:02:51 +02:00
add paths to ranges (#1997)
#### Is this adding or improving a _feature_ or fixing a _bug_? Feature. #### What's the new behavior? This pull request adds paths to `Range` objects, including the selection. The paths and keys are kept in sync automatically, so that you can use whichever is ideal for your use case. This should allow us to use paths for lots of the internal logic, which are much quicker to work with than keys since they avoid having to lookup the key in the document and can just traverse right to the node in question. #### How does this change work? `Range` objects have two new properties: ```js range.anchorPath range.focusPath ``` (Eventually these will be `range.anchor.path` and `range.focus.path` when points are introduced.) When operations occur and whenever ranges are created/normalized, the paths are updated and kept in sync with the keys. #### Have you checked that...? <!-- Please run through this checklist for your pull request: --> * [x] The new code matches the existing patterns and styles. * [x] The tests pass with `yarn test`. * [x] The linter passes with `yarn lint`. (Fix errors with `yarn prettier`.) * [x] The relevant examples still work. (Run examples with `yarn watch`.) #### Does this fix any issues or need any specific reviewers? Fixes: https://github.com/ianstormtaylor/slate/issues/1408 Fixes: https://github.com/ianstormtaylor/slate/issues/1567
This commit is contained in:
@@ -1,91 +1,37 @@
|
||||
/**
|
||||
* Dependencies.
|
||||
*/
|
||||
|
||||
import assert from 'assert'
|
||||
import fs from 'fs'
|
||||
import { Value } from 'slate'
|
||||
import { Value, KeyUtils } from 'slate'
|
||||
import { basename, extname, resolve } from 'path'
|
||||
|
||||
/**
|
||||
* Tests.
|
||||
*/
|
||||
beforeEach(KeyUtils.resetGenerator)
|
||||
|
||||
describe('slate-hyperscript', () => {
|
||||
describe('default settings', () => {
|
||||
const dir = resolve(__dirname, './default')
|
||||
const tests = fs
|
||||
.readdirSync(dir)
|
||||
const dir = resolve(__dirname, './fixtures')
|
||||
const tests = fs
|
||||
.readdirSync(dir)
|
||||
.filter(t => t[0] != '.')
|
||||
.map(t => basename(t, extname(t)))
|
||||
|
||||
for (const test of tests) {
|
||||
it(test, async () => {
|
||||
const module = require(resolve(dir, test))
|
||||
const { input, output, options } = module
|
||||
const actual = input.toJSON(options)
|
||||
const expected = Value.isValue(output) ? output.toJSON() : output
|
||||
assert.deepEqual(actual, expected)
|
||||
})
|
||||
}
|
||||
|
||||
describe.skip('decorations', () => {
|
||||
const decDir = resolve(__dirname, './decorations')
|
||||
const decTests = fs
|
||||
.readdirSync(decDir)
|
||||
.filter(t => t[0] != '.')
|
||||
.map(t => basename(t, extname(t)))
|
||||
|
||||
for (const test of tests) {
|
||||
for (const test of decTests) {
|
||||
it(test, async () => {
|
||||
const module = require(resolve(dir, test))
|
||||
const { input, output } = module
|
||||
|
||||
const actual = input.toJSON()
|
||||
const expected = Value.isValue(output) ? output.toJSON() : output
|
||||
assert.deepEqual(actual, expected)
|
||||
if (module.test) module.test()
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
describe('custom tags', () => {
|
||||
const dir = resolve(__dirname, './custom')
|
||||
const tests = fs
|
||||
.readdirSync(dir)
|
||||
.filter(t => t[0] != '.')
|
||||
.map(t => basename(t, extname(t)))
|
||||
|
||||
for (const test of tests) {
|
||||
it(test, async () => {
|
||||
const module = require(resolve(dir, test))
|
||||
const { input, output } = module
|
||||
|
||||
const actual = input.toJSON()
|
||||
const expected = Value.isValue(output) ? output.toJSON() : output
|
||||
assert.deepEqual(actual, expected)
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
describe('selections', () => {
|
||||
const dir = resolve(__dirname, './selections')
|
||||
const tests = fs
|
||||
.readdirSync(dir)
|
||||
.filter(t => t[0] != '.')
|
||||
.map(t => basename(t, extname(t)))
|
||||
|
||||
for (const test of tests) {
|
||||
it(test, async () => {
|
||||
const module = require(resolve(dir, test))
|
||||
const { input, output, expectSelection } = module
|
||||
|
||||
// ensure deserialization was okay
|
||||
const actual = input.toJSON()
|
||||
const expected = Value.isValue(output) ? output.toJSON() : output
|
||||
assert.deepEqual(actual, expected)
|
||||
|
||||
// ensure expected properties of selection match
|
||||
Object.keys(expectSelection).forEach(prop => {
|
||||
assert.equal(input.selection[prop], expectSelection[prop])
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
describe('decorations', () => {
|
||||
const dir = resolve(__dirname, './decorations')
|
||||
const tests = fs
|
||||
.readdirSync(dir)
|
||||
.filter(t => t[0] != '.')
|
||||
.map(t => basename(t, extname(t)))
|
||||
|
||||
for (const test of tests) {
|
||||
it(test, async () => {
|
||||
const module = require(resolve(dir, test))
|
||||
const module = require(resolve(decDir, test))
|
||||
const { input, output, expectDecorations } = module
|
||||
|
||||
// ensure deserialization was okay
|
||||
@@ -107,23 +53,4 @@ describe('slate-hyperscript', () => {
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
describe('normalize', () => {
|
||||
const dir = resolve(__dirname, './normalize')
|
||||
const tests = fs
|
||||
.readdirSync(dir)
|
||||
.filter(t => t[0] != '.')
|
||||
.map(t => basename(t, extname(t)))
|
||||
|
||||
for (const test of tests) {
|
||||
it(test, async () => {
|
||||
const module = require(resolve(dir, test))
|
||||
const { input, output } = module
|
||||
|
||||
const actual = Value.isValue(input) ? input.toJSON() : input
|
||||
const expected = Value.isValue(output) ? output.toJSON() : output
|
||||
assert.deepEqual(actual, expected)
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user