flow exact objects by default (#1249)

This commit is contained in:
Max Haubenstock
2022-02-08 16:43:59 -05:00
committed by acywatson
parent 2020bbadd5
commit bf6bf30504
13 changed files with 34 additions and 16 deletions

View File

@ -14,6 +14,7 @@ untyped-type-import=error
[options]
server.max_workers=4
exact_by_default=true
module.name_mapper='^lexical$' -> '<PROJECT_ROOT>/packages/lexical/src/index.js'

View File

@ -42,7 +42,7 @@ function readTextFileFromSystem(callback: (text: string) => void) {
export function exportFile(
editor: LexicalEditor,
config?: $ReadOnly<{source?: string, fileName?: string}> = {},
config?: $ReadOnly<{source?: string, fileName?: string}> = Object.freeze({}),
) {
const now = new Date();
const editorState = editor.getEditorState();

View File

@ -18,7 +18,10 @@ type SettingsContextShape = {
setOption: (name: SettingName, value: boolean) => void,
};
const Context: React$Context<SettingsContextShape> = createContext({});
const Context: React$Context<SettingsContextShape> = createContext({
settings: {},
setOption: (_, __) => {},
});
export const SettingsContext = ({
children,

View File

@ -17,7 +17,9 @@ type ContextShape = {
historyState?: HistoryState,
};
const Context: React$Context<ContextShape> = createContext({});
const Context: React$Context<ContextShape> = createContext({
historyState: {current: null, redoStack: [], undoStack: []},
});
export const SharedHistoryContext = ({
children,

View File

@ -117,13 +117,7 @@ export class ExcalidrawNode extends DecoratorNode {
}
decorate(editor: LexicalEditor): React$Node {
return (
<ExcalidrawComponent
nodeKey={this.getKey()}
state={this.__state}
elements={[]}
/>
);
return <ExcalidrawComponent nodeKey={this.getKey()} state={this.__state} />;
}
}

View File

@ -434,7 +434,7 @@ function ImageComponent({
<CollaborationPlugin
id={decoratorEditor.id}
providerFactory={createWebsocketProvider}
initEditorState={false}
skipInit={false}
/>
) : (
<HistoryPlugin externalHistoryState={historyState} />

View File

@ -282,7 +282,7 @@ function StickyComponent({
<CollaborationPlugin
id={decoratorEditor.id}
providerFactory={createWebsocketProvider}
initEditorState={false}
skipInit={false}
/>
) : (
<HistoryPlugin externalHistoryState={historyState} />

View File

@ -35,7 +35,14 @@ export default function useLexical<EditorContext>(editorConfig?: {
const onError =
(editorConfig !== undefined && editorConfig.onError) ||
defaultOnErrorHandler;
const editor = useMemo(() => createEditor(editorConfig), [editorConfig]);
const editor = useMemo(() => {
if (editorConfig !== undefined) {
// eslint-disable-next-line no-unused-vars
const {onError: _onError, ...config} = editorConfig;
return createEditor(config);
}
return createEditor(editorConfig);
}, [editorConfig]);
const [rootElementRef, showPlaceholder] = useLexicalEditor(editor, onError);
return [editor, rootElementRef, showPlaceholder];

View File

@ -37,7 +37,7 @@ type OptionalProps = {
export function useCharacterLimit(
editor: LexicalEditor,
maxCharacters: number,
optional: OptionalProps = {},
optional: OptionalProps = Object.freeze({}),
): void {
const {
strlen = (input) => input.length, // UTF-16

View File

@ -47,6 +47,7 @@ export type CursorSelection = {
key: NodeKey,
offset: number,
},
name: HTMLSpanElement,
};
export type Cursor = {

View File

@ -149,6 +149,7 @@ export type RootListener = (
prevRootElement: null | HTMLElement,
) => void;
export type TextContentListener = (text: string) => void;
export type TextMutationListener = (text: Text) => void;
export type CommandListener = (
type: string,
payload: CommandPayload,
@ -178,6 +179,7 @@ type Listeners = {
root: Set<RootListener>,
update: Set<UpdateListener>,
command: Array<Set<CommandListener>>,
textmutation: Set<TextMutationListener>,
};
export type ListenerType =
@ -186,6 +188,7 @@ export type ListenerType =
| 'root'
| 'decorator'
| 'textcontent'
| 'textmutation'
| 'command';
export type TransformerType = 'text' | 'decorator' | 'element' | 'root';
@ -353,6 +356,7 @@ class BaseLexicalEditor {
decorator: new Set(),
error: new Set(),
textcontent: new Set(),
textmutation: new Set(),
root: new Set(),
update: new Set(),
command: [new Set(), new Set(), new Set(), new Set(), new Set()],

View File

@ -56,14 +56,19 @@ type RootElementEvents = Array<
[string, {} | ((event: Event, editor: LexicalEditor) => void)],
>;
const PASS_THROUGH_COMMAND = {};
const PASS_THROUGH_COMMAND = Object.freeze({});
const rootElementEvents: RootElementEvents = [
['selectionchange', onSelectionChange],
// $FlowIgnore bad event inheritance
['keydown', onKeyDown],
// $FlowIgnore bad event inheritance
['compositionstart', onCompositionStart],
// $FlowIgnore bad event inheritance
['compositionend', onCompositionEnd],
// $FlowIgnore bad event inheritance
['input', onInput],
// $FlowIgnore bad event inheritance
['click', onClick],
['cut', PASS_THROUGH_COMMAND],
['copy', PASS_THROUGH_COMMAND],
@ -74,6 +79,7 @@ const rootElementEvents: RootElementEvents = [
];
if (CAN_USE_BEFORE_INPUT) {
// $FlowIgnore bad event inheritance
rootElementEvents.push(['beforeinput', onBeforeInput]);
} else {
rootElementEvents.push(['drop', PASS_THROUGH_COMMAND]);

View File

@ -138,7 +138,7 @@ export function internalCreateNodeFromParse(
parsedNodeMap: ParsedNodeMap,
editor: LexicalEditor,
parentKey: null | NodeKey,
state: NodeParserState = {},
state: NodeParserState = {originalSelection: null},
): LexicalNode {
const nodeType = parsedNode.__type;
const registeredNode = editor._registeredNodes.get(nodeType);