1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-01 04:50:27 +02:00

Merge pull request #462 from ianstormtaylor/add-reset-key

add resetKeyGenerator util
This commit is contained in:
Ian Storm Taylor
2016-11-29 17:30:29 -08:00
committed by GitHub
3 changed files with 37 additions and 8 deletions

View File

@@ -11,6 +11,7 @@ import {
Utility functions that ship with Slate that may be useful for certain use cases.
- [`findDOMNode`](#finddomnode)
- [`resetKeyGenerator`](#resetkeygenerator)
- [`setKeyGenerator`](#setkeygenerator)
@@ -21,6 +22,11 @@ Utility functions that ship with Slate that may be useful for certain use cases.
Allows you to find the DOM node for a Slate [`Node`](../models/node.md) by passing its `key` string. Modelled after React's built-in `findDOMNode` helper.
### `resetKeyGenerator`
`resetkeygenerator() => Void`
Resets Slate's internal key generating function to its default state. This is useful for server-side rendering, or anywhere you want to ensure fresh, deterministic creation of keys.
### `setKeyGenerator`
`setKeyGenerator(generator: Function) => Void`

View File

@@ -41,7 +41,7 @@ import Transforms from './transforms'
*/
import findDOMNode from './utils/find-dom-node'
import { setKeyGenerator } from './utils/generate-key'
import { resetKeyGenerator, setKeyGenerator } from './utils/generate-key'
/**
* Export.
@@ -68,6 +68,7 @@ export {
Text,
Transforms,
findDOMNode,
resetKeyGenerator,
setKeyGenerator
}
@@ -90,5 +91,6 @@ export default {
Text,
Transforms,
findDOMNode,
resetKeyGenerator,
setKeyGenerator
}

View File

@@ -1,17 +1,22 @@
import uniqueId from 'lodash/uniqueId'
/**
* Default the generator function to Lodash's implementation, which just returns
* incrementing numbers as strings.
* An auto-incrementing index for generating keys.
*
* @type {Number}
*/
let n
/**
* The global key generating function.
*
* @type {Function}
*/
let generate = uniqueId
let generate
/**
* Create a key.
* Generate a key.
*
* @return {String}
*/
@@ -30,6 +35,21 @@ function setKeyGenerator(func) {
generate = func
}
/**
* Reset the key generating function to its initial state.
*/
function resetKeyGenerator() {
n = 0
generate = () => `${n++}`
}
/**
* Set the initial state.
*/
resetKeyGenerator()
/**
* Export.
*
@@ -38,5 +58,6 @@ function setKeyGenerator(func) {
export {
generateKey as default,
setKeyGenerator
setKeyGenerator,
resetKeyGenerator
}