Skip to content

Commit

Permalink
Implement some basic attributes of the EditorView API
Browse files Browse the repository at this point in the history
  • Loading branch information
tilgovi committed Jul 28, 2023
1 parent 3177534 commit b6e7809
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions src/components/EditorView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,19 @@ export type EditorProps = Omit<
export type Props = EditorProps &
DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>;

export function EditorView({
children,
editable,
keymap = {},
nodeViews = {},
dispatchTransaction: dispatchProp,
decorations = DecorationSet.empty,
defaultState,
state: stateProp,
...mountProps
}: Props) {
export function EditorView(props: Props) {
const {
children,
editable: editableProp,
keymap = {},
nodeViews = {},
dispatchTransaction: dispatchProp,
decorations = DecorationSet.empty,
defaultState,
state: stateProp,
...mountProps
} = props;

const [internalState, setInternalState] = useState<EditorState | null>(
defaultState ?? null
);
Expand All @@ -113,7 +115,7 @@ export function EditorView({
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const state = stateProp ?? internalState!;

const dispatchTransaction = useMemo(
const dispatch = useMemo(
() =>
dispatchProp ??
((tr: Transaction) => {
Expand All @@ -122,12 +124,12 @@ export function EditorView({
[dispatchProp]
);

const mountRef = useContentEditable(state, dispatchTransaction);
const mountRef = useContentEditable(state, dispatch);

useSyncSelection(state, dispatchTransaction, posToDesc, domToDesc);
useSyncSelection(state, dispatch, posToDesc, domToDesc);

const onKeyDown: KeyboardEventHandler = (event) => {
if (keydownHandler(keymap)(state, dispatchTransaction, event.nativeEvent)) {
if (keydownHandler(keymap)(state, dispatch, event.nativeEvent)) {
event.preventDefault();
}
};
Expand Down Expand Up @@ -294,10 +296,12 @@ export function EditorView({
return <>{elements}</>;
}

const editable = editableProp ? editableProp(state) : true;

const content = buildReactTree(
<div
ref={mountRef}
contentEditable={editable ? editable(state) : true}
contentEditable={editable}
suppressContentEditableWarning={true}
onKeyDown={onKeyDown}
{...mountProps}
Expand All @@ -308,12 +312,18 @@ export function EditorView({
);

const contextValue = useMemo<EditorViewT>(
// @ts-expect-error - EditorView API not fully implemented yet
() => ({

Check failure on line 315 in src/components/EditorView.tsx

View workflow job for this annotation

GitHub Actions / check

Type '{ dom: HTMLDivElement; editable: boolean; state: EditorState; dispatch: (tr: Transaction) => void; props: { editable: ((this: any, state: EditorState) => boolean) | undefined; state: EditorState; dispatchTransaction: ((tr: Transaction) => void) | undefined; }; }' is missing the following properties from type 'EditorView': _props, directPlugins, _root, mounted, and 25 more.
dom: mountRef.current!,

Check warning on line 316 in src/components/EditorView.tsx

View workflow job for this annotation

GitHub Actions / check

Forbidden non-null assertion
editable,
state,
dispatchTransaction,
dispatch,
props: {
editable: editableProp,
state: stateProp ?? defaultState,
dispatchTransaction: dispatchProp,
},
}),
[state, dispatchTransaction]
[editable, editableProp, state, stateProp, dispatch, dispatchProp]

Check warning on line 326 in src/components/EditorView.tsx

View workflow job for this annotation

GitHub Actions / check

React Hook useMemo has missing dependencies: 'defaultState' and 'mountRef'. Either include them or remove the dependency array
);

return (
Expand Down

0 comments on commit b6e7809

Please sign in to comment.