slate-react 0.73.0 → 0.74.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/components/slate.d.ts.map +1 -1
- package/dist/hooks/use-slate-selector.d.ts +32 -0
- package/dist/hooks/use-slate-selector.d.ts.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.es.js +128 -4
- package/dist/index.es.js.map +1 -1
- package/dist/index.js +141 -2
- package/dist/index.js.map +1 -1
- package/dist/slate-react.js +141 -2
- package/dist/slate-react.min.js +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# slate-react
|
|
2
2
|
|
|
3
|
+
## 0.74.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#4841](https://github.com/ianstormtaylor/slate/pull/4841) [`47f2403e`](https://github.com/ianstormtaylor/slate/commit/47f2403e3a46d84b8d8f99c6e2bf41f2699e30df) Thanks [@Fibs7000](https://github.com/Fibs7000)! - Added redux-style useSlateSelector to improve and prevent unneccessary rerendering with the useSlate hook
|
|
8
|
+
|
|
3
9
|
## 0.73.0
|
|
4
10
|
|
|
5
11
|
### Patch Changes
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"slate.d.ts","sourceRoot":"","sources":["../packages/slate-react/src/components/slate.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA4D,MAAM,OAAO,CAAA;AAChF,OAAO,EAAyB,UAAU,EAAE,MAAM,OAAO,CAAA;AACzD,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAA;
|
|
1
|
+
{"version":3,"file":"slate.d.ts","sourceRoot":"","sources":["../packages/slate-react/src/components/slate.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA4D,MAAM,OAAO,CAAA;AAChF,OAAO,EAAyB,UAAU,EAAE,MAAM,OAAO,CAAA;AACzD,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAA;AAWpD;;;GAGG;AAEH,eAAO,MAAM,KAAK;YACR,WAAW;WACZ,UAAU,EAAE;cACT,KAAK,CAAC,SAAS;sBACP,UAAU,EAAE,KAAK,IAAI;iBA4ExC,CAAA"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { Editor } from 'slate';
|
|
3
|
+
declare type EditorChangeHandler = (editor: Editor) => void;
|
|
4
|
+
/**
|
|
5
|
+
* A React context for sharing the editor selector context in a way to control rerenders
|
|
6
|
+
*/
|
|
7
|
+
export declare const SlateSelectorContext: import("react").Context<{
|
|
8
|
+
getSlate: () => Editor;
|
|
9
|
+
addEventListener: (callback: EditorChangeHandler) => () => void;
|
|
10
|
+
}>;
|
|
11
|
+
/**
|
|
12
|
+
* use redux style selectors to prevent rerendering on every keystroke.
|
|
13
|
+
* Bear in mind rerendering can only prevented if the returned value is a value type or for reference types (e.g. objects and arrays) add a custom equality function.
|
|
14
|
+
*
|
|
15
|
+
* Example:
|
|
16
|
+
* ```
|
|
17
|
+
* const isSelectionActive = useSlateSelector(editor => Boolean(editor.selection));
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare function useSlateSelector<T>(selector: (editor: Editor) => T, equalityFn?: (a: T, b: T) => boolean): T;
|
|
21
|
+
/**
|
|
22
|
+
* Create selector context with editor updating on every editor change
|
|
23
|
+
*/
|
|
24
|
+
export declare function getSelectorContext(editor: Editor): {
|
|
25
|
+
selectorContext: {
|
|
26
|
+
getSlate: () => import("..").ReactEditor;
|
|
27
|
+
addEventListener: (callback: EditorChangeHandler) => () => void;
|
|
28
|
+
};
|
|
29
|
+
onChange: (editor: Editor) => void;
|
|
30
|
+
};
|
|
31
|
+
export {};
|
|
32
|
+
//# sourceMappingURL=use-slate-selector.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-slate-selector.d.ts","sourceRoot":"","sources":["../packages/slate-react/src/hooks/use-slate-selector.tsx"],"names":[],"mappings":";AAQA,OAAO,EAAE,MAAM,EAAE,MAAM,OAAO,CAAA;AAO9B,aAAK,mBAAmB,GAAG,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,CAAA;AACnD;;GAEG;AAEH,eAAO,MAAM,oBAAoB;cACrB,MAAM,MAAM;iCACO,mBAAmB,KAAK,MAAM,IAAI;EACpD,CAAA;AAIb;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAChC,QAAQ,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,CAAC,EAC/B,UAAU,GAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,OAAqB,KAuElD;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM;;;qCAed,mBAAmB;;uBARd,MAAM;EAiB7C"}
|
package/dist/index.d.ts
CHANGED
|
@@ -11,6 +11,7 @@ export { useFocused } from './hooks/use-focused';
|
|
|
11
11
|
export { useReadOnly } from './hooks/use-read-only';
|
|
12
12
|
export { useSelected } from './hooks/use-selected';
|
|
13
13
|
export { useSlate } from './hooks/use-slate';
|
|
14
|
+
export { useSlateSelector } from './hooks/use-slate-selector';
|
|
14
15
|
export { ReactEditor } from './plugin/react-editor';
|
|
15
16
|
export { withReact } from './plugin/with-react';
|
|
16
17
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["packages/slate-react/src/index.ts"],"names":[],"mappings":";AAMA,eAAO,MAAM,QAAQ,uEAAiD,CAAA;AACtE,OAAO,EACL,QAAQ,IAAI,eAAe,EAC3B,kBAAkB,EAClB,eAAe,EACf,sBAAsB,EACtB,kBAAkB,GACnB,MAAM,uBAAuB,CAAA;AAC9B,OAAO,EAAE,eAAe,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AAA;AAC5C,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAA;AAG7D,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AACnD,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAA"}
|
package/dist/index.es.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { useLayoutEffect, useEffect, useRef, useState, Fragment, useContext, createContext, useMemo, useCallback } from 'react';
|
|
1
|
+
import React, { useLayoutEffect, useEffect, useRef, useState, Fragment, useContext, createContext, useMemo, useCallback, useReducer } from 'react';
|
|
2
2
|
import { Path, Node, Editor, Text as Text$1, Range, Element as Element$1, Transforms } from 'slate';
|
|
3
3
|
import getDirection from 'direction';
|
|
4
4
|
import debounce from 'lodash/debounce';
|
|
@@ -3884,6 +3884,123 @@ var useFocused = () => {
|
|
|
3884
3884
|
return useContext(FocusedContext);
|
|
3885
3885
|
};
|
|
3886
3886
|
|
|
3887
|
+
function isError(error) {
|
|
3888
|
+
return error instanceof Error;
|
|
3889
|
+
}
|
|
3890
|
+
/**
|
|
3891
|
+
* A React context for sharing the editor selector context in a way to control rerenders
|
|
3892
|
+
*/
|
|
3893
|
+
|
|
3894
|
+
|
|
3895
|
+
var SlateSelectorContext = /*#__PURE__*/createContext({});
|
|
3896
|
+
|
|
3897
|
+
var refEquality = (a, b) => a === b;
|
|
3898
|
+
/**
|
|
3899
|
+
* use redux style selectors to prevent rerendering on every keystroke.
|
|
3900
|
+
* Bear in mind rerendering can only prevented if the returned value is a value type or for reference types (e.g. objects and arrays) add a custom equality function.
|
|
3901
|
+
*
|
|
3902
|
+
* Example:
|
|
3903
|
+
* ```
|
|
3904
|
+
* const isSelectionActive = useSlateSelector(editor => Boolean(editor.selection));
|
|
3905
|
+
* ```
|
|
3906
|
+
*/
|
|
3907
|
+
|
|
3908
|
+
|
|
3909
|
+
function useSlateSelector(selector) {
|
|
3910
|
+
var equalityFn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : refEquality;
|
|
3911
|
+
var [, forceRender] = useReducer(s => s + 1, 0);
|
|
3912
|
+
var context = useContext(SlateSelectorContext);
|
|
3913
|
+
|
|
3914
|
+
if (!context) {
|
|
3915
|
+
throw new Error("The `useSlateSelector` hook must be used inside the <Slate> component's context.");
|
|
3916
|
+
}
|
|
3917
|
+
|
|
3918
|
+
var {
|
|
3919
|
+
getSlate,
|
|
3920
|
+
addEventListener
|
|
3921
|
+
} = context;
|
|
3922
|
+
var latestSubscriptionCallbackError = useRef();
|
|
3923
|
+
var latestSelector = useRef(() => null);
|
|
3924
|
+
var latestSelectedState = useRef(null);
|
|
3925
|
+
var selectedState;
|
|
3926
|
+
|
|
3927
|
+
try {
|
|
3928
|
+
if (selector !== latestSelector.current || latestSubscriptionCallbackError.current) {
|
|
3929
|
+
selectedState = selector(getSlate());
|
|
3930
|
+
} else {
|
|
3931
|
+
selectedState = latestSelectedState.current;
|
|
3932
|
+
}
|
|
3933
|
+
} catch (err) {
|
|
3934
|
+
if (latestSubscriptionCallbackError.current && isError(err)) {
|
|
3935
|
+
err.message += "\nThe error may be correlated with this previous error:\n".concat(latestSubscriptionCallbackError.current.stack, "\n\n");
|
|
3936
|
+
}
|
|
3937
|
+
|
|
3938
|
+
throw err;
|
|
3939
|
+
}
|
|
3940
|
+
|
|
3941
|
+
useIsomorphicLayoutEffect(() => {
|
|
3942
|
+
latestSelector.current = selector;
|
|
3943
|
+
latestSelectedState.current = selectedState;
|
|
3944
|
+
latestSubscriptionCallbackError.current = undefined;
|
|
3945
|
+
});
|
|
3946
|
+
useIsomorphicLayoutEffect(() => {
|
|
3947
|
+
function checkForUpdates() {
|
|
3948
|
+
try {
|
|
3949
|
+
var newSelectedState = latestSelector.current(getSlate());
|
|
3950
|
+
|
|
3951
|
+
if (equalityFn(newSelectedState, latestSelectedState.current)) {
|
|
3952
|
+
return;
|
|
3953
|
+
}
|
|
3954
|
+
|
|
3955
|
+
latestSelectedState.current = newSelectedState;
|
|
3956
|
+
} catch (err) {
|
|
3957
|
+
// we ignore all errors here, since when the component
|
|
3958
|
+
// is re-rendered, the selectors are called again, and
|
|
3959
|
+
// will throw again, if neither props nor store state
|
|
3960
|
+
// changed
|
|
3961
|
+
latestSubscriptionCallbackError.current = err;
|
|
3962
|
+
}
|
|
3963
|
+
|
|
3964
|
+
forceRender();
|
|
3965
|
+
}
|
|
3966
|
+
|
|
3967
|
+
var unsubscribe = addEventListener(checkForUpdates);
|
|
3968
|
+
checkForUpdates();
|
|
3969
|
+
return () => unsubscribe();
|
|
3970
|
+
}, // don't rerender on equalityFn change since we want to be able to define it inline
|
|
3971
|
+
[addEventListener, getSlate]);
|
|
3972
|
+
return selectedState;
|
|
3973
|
+
}
|
|
3974
|
+
/**
|
|
3975
|
+
* Create selector context with editor updating on every editor change
|
|
3976
|
+
*/
|
|
3977
|
+
|
|
3978
|
+
function getSelectorContext(editor) {
|
|
3979
|
+
var eventListeners = useRef([]).current;
|
|
3980
|
+
var slateRef = useRef({
|
|
3981
|
+
editor
|
|
3982
|
+
}).current;
|
|
3983
|
+
var onChange = useCallback(editor => {
|
|
3984
|
+
slateRef.editor = editor;
|
|
3985
|
+
eventListeners.forEach(listener => listener(editor));
|
|
3986
|
+
}, []);
|
|
3987
|
+
var selectorContext = useMemo(() => {
|
|
3988
|
+
return {
|
|
3989
|
+
getSlate: () => slateRef.editor,
|
|
3990
|
+
addEventListener: callback => {
|
|
3991
|
+
eventListeners.push(callback);
|
|
3992
|
+
return () => {
|
|
3993
|
+
eventListeners.splice(eventListeners.indexOf(callback), 1);
|
|
3994
|
+
};
|
|
3995
|
+
}
|
|
3996
|
+
};
|
|
3997
|
+
}, [eventListeners, slateRef]);
|
|
3998
|
+
return {
|
|
3999
|
+
selectorContext,
|
|
4000
|
+
onChange
|
|
4001
|
+
};
|
|
4002
|
+
}
|
|
4003
|
+
|
|
3887
4004
|
var _excluded = ["editor", "children", "onChange", "value"];
|
|
3888
4005
|
/**
|
|
3889
4006
|
* A wrapper around the provider to handle `onChange` events, because the editor
|
|
@@ -3913,9 +4030,14 @@ var Slate = props => {
|
|
|
3913
4030
|
Object.assign(editor, rest);
|
|
3914
4031
|
return [editor];
|
|
3915
4032
|
});
|
|
4033
|
+
var {
|
|
4034
|
+
selectorContext,
|
|
4035
|
+
onChange: handleSelectorChange
|
|
4036
|
+
} = getSelectorContext(editor);
|
|
3916
4037
|
var onContextChange = useCallback(() => {
|
|
3917
4038
|
onChange(editor.children);
|
|
3918
4039
|
setContext([editor]);
|
|
4040
|
+
handleSelectorChange(editor);
|
|
3919
4041
|
}, [onChange]);
|
|
3920
4042
|
EDITOR_TO_ON_CHANGE.set(editor, onContextChange);
|
|
3921
4043
|
useEffect(() => {
|
|
@@ -3946,13 +4068,15 @@ var Slate = props => {
|
|
|
3946
4068
|
document.removeEventListener('blur', fn, true);
|
|
3947
4069
|
};
|
|
3948
4070
|
}, []);
|
|
3949
|
-
return /*#__PURE__*/React.createElement(
|
|
4071
|
+
return /*#__PURE__*/React.createElement(SlateSelectorContext.Provider, {
|
|
4072
|
+
value: selectorContext
|
|
4073
|
+
}, /*#__PURE__*/React.createElement(SlateContext.Provider, {
|
|
3950
4074
|
value: context
|
|
3951
4075
|
}, /*#__PURE__*/React.createElement(EditorContext.Provider, {
|
|
3952
4076
|
value: editor
|
|
3953
4077
|
}, /*#__PURE__*/React.createElement(FocusedContext.Provider, {
|
|
3954
4078
|
value: isFocused
|
|
3955
|
-
}, children)));
|
|
4079
|
+
}, children))));
|
|
3956
4080
|
};
|
|
3957
4081
|
|
|
3958
4082
|
/**
|
|
@@ -4279,5 +4403,5 @@ var withReact = editor => {
|
|
|
4279
4403
|
// Components
|
|
4280
4404
|
var Editable = IS_ANDROID ? AndroidEditable : Editable$1;
|
|
4281
4405
|
|
|
4282
|
-
export { AndroidEditable, Editable$1 as DefaultEditable, DefaultElement, DefaultLeaf, DefaultPlaceholder, Editable, ReactEditor, Slate, useEditor, useFocused, useReadOnly, useSelected, useSlate, useSlateStatic, withReact };
|
|
4406
|
+
export { AndroidEditable, Editable$1 as DefaultEditable, DefaultElement, DefaultLeaf, DefaultPlaceholder, Editable, ReactEditor, Slate, useEditor, useFocused, useReadOnly, useSelected, useSlate, useSlateSelector, useSlateStatic, withReact };
|
|
4283
4407
|
//# sourceMappingURL=index.es.js.map
|