Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use consistent module layout and naming #97

Merged
merged 5 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .yarn/versions/fb01eea5.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
releases:
"@nytimes/react-prosemirror": minor
19 changes: 19 additions & 0 deletions src/components/Editor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from "react";
import type { ReactNode } from "react";

import { useEditorView } from "../hooks/useEditorView.js";
import type { UseEditorViewOptions } from "../hooks/useEditorView.js";

import { EditorContext } from "./EditorContext.js";

export interface EditorProps extends UseEditorViewOptions {
mount: HTMLElement | null;
children?: ReactNode | null;
}

export function Editor({ mount, children, ...options }: EditorProps) {
const value = useEditorView(mount, options);
return (
<EditorContext.Provider value={value}>{children}</EditorContext.Provider>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,3 @@ export interface EditorContextValue {
export const EditorContext = createContext(
null as unknown as EditorContextValue
);

export const EditorProvider = EditorContext.Provider;
69 changes: 69 additions & 0 deletions src/components/LayoutGroup.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import React, { useCallback, useLayoutEffect, useRef } from "react";
import type { EffectCallback } from "react";

import { useForceUpdate } from "../hooks/useForceUpdate.js";

import { LayoutGroupContext } from "./LayoutGroupContext.js";

export interface LayoutGroupProps {
children: React.ReactNode;
}

/**
* Provides a boundary for grouping layout effects.
*
* Descendant components can invoke the `useLayoutGroupEffect` hook to register
* effects that run after all descendants within the group have processed their
* regular layout effects.
*/
export function LayoutGroup({ children }: LayoutGroupProps) {
const createQueue = useRef(new Set<() => void>()).current;
const destroyQueue = useRef(new Set<() => void>()).current;

const forceUpdate = useForceUpdate();
const isUpdatePending = useRef(true);

const ensureFlush = useCallback(() => {
if (!isUpdatePending.current) {
forceUpdate();
isUpdatePending.current = true;
}
}, [forceUpdate]);

const register = useCallback<typeof useLayoutEffect>(
(effect: EffectCallback) => {
let destroy: ReturnType<EffectCallback>;
const create = () => {
destroy = effect();
};

createQueue.add(create);
ensureFlush();

return () => {
createQueue.delete(create);
if (destroy) {
destroyQueue.add(destroy);
ensureFlush();
}
};
},
[createQueue, destroyQueue, ensureFlush]
);

useLayoutEffect(() => {
isUpdatePending.current = false;
createQueue.forEach((create) => create());
createQueue.clear();
return () => {
destroyQueue.forEach((destroy) => destroy());
destroyQueue.clear();
};
});

return (
<LayoutGroupContext.Provider value={register}>
{children}
</LayoutGroupContext.Provider>
);
}
10 changes: 10 additions & 0 deletions src/components/LayoutGroupContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { createContext } from "react";
import type { EffectCallback } from "react";

export interface LayoutGroupContextValue {
(effect: EffectCallback): ReturnType<EffectCallback>;
}

export const LayoutGroupContext = createContext(
null as unknown as LayoutGroupContextValue
);
36 changes: 36 additions & 0 deletions src/components/NodeViews.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React, { useState } from "react";
import type { ReactPortal } from "react";

import { useEditorEffect } from "../hooks/useEditorEffect.js";
import { ROOT_NODE_KEY } from "../plugins/react.js";

import { NodeViewsContext } from "./NodeViewsContext.js";
import type { NodeViewsContextValue } from "./NodeViewsContext.js";

type NodeViewsProps = {
portals: NodeViewsContextValue;
};

export function NodeViews({ portals }: NodeViewsProps) {
const rootRegisteredPortals = portals[ROOT_NODE_KEY];
const [rootPortals, setRootPortals] = useState<ReactPortal[]>(
rootRegisteredPortals?.map(({ portal }) => portal) ?? []
);

// `getPos` is technically derived from the EditorView
// state, so it's not safe to call until after the EditorView
// has been updated
useEditorEffect(() => {
setRootPortals(
rootRegisteredPortals
?.sort((a, b) => a.getPos() - b.getPos())
.map(({ portal }) => portal) ?? []
);
}, [rootRegisteredPortals]);

return (
<NodeViewsContext.Provider value={portals}>
{rootPortals}
</NodeViewsContext.Provider>
);
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { ReactPortal, createContext } from "react";

import { NodeKey } from "../plugins/react.js";

export type RegisteredPortal = {
type NodeViewRegistration = {
getPos: () => number;
portal: ReactPortal;
};

export type PortalRegistry = Record<NodeKey, RegisteredPortal[]>;
export interface NodeViewsContextValue {
[key: symbol | string]: NodeViewRegistration[];
}

/**
* A map of node view keys to portals.
* A context containing a map of node view keys to portals.
*
* Each node view registers a portal under its parent's
* key. Each can then retrieve the list of portals under their
* key, allowing portals to be rendered with the appropriate
* hierarchy.
*/
export const PortalRegistryContext = createContext<PortalRegistry>(
null as unknown as PortalRegistry
export const NodeViewsContext = createContext(
null as unknown as NodeViewsContextValue
);
25 changes: 4 additions & 21 deletions src/components/ProseMirror.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,8 @@
import type { EditorState, Plugin, Transaction } from "prosemirror-state";
import type { EditorProps, EditorView } from "prosemirror-view";
import React from "react";
import type { ReactNode } from "react";

import { EditorProvider } from "../contexts/EditorContext.js";
import { LayoutGroup } from "../contexts/LayoutGroup.js";
import { useEditorView } from "../hooks/useEditorView.js";

interface Props extends EditorProps {
mount: HTMLElement | null;
children?: ReactNode | null;
defaultState?: EditorState;
state?: EditorState;
plugins?: readonly Plugin[];
dispatchTransaction?(this: EditorView, tr: Transaction): void;
}

function Editor({ mount, children, ...props }: Props) {
const value = useEditorView(mount, props);
return <EditorProvider value={value}>{children}</EditorProvider>;
}
import { Editor } from "./Editor.js";
import type { EditorProps } from "./Editor.js";
import { LayoutGroup } from "./LayoutGroup.js";

/**
* Renders the ProseMirror View onto a DOM mount.
Expand All @@ -42,7 +25,7 @@ function Editor({ mount, children, ...props }: Props) {
* }
* ```
*/
export function ProseMirror(props: Props) {
export function ProseMirror(props: EditorProps) {
return (
<LayoutGroup>
<Editor {...props} />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { act, render, screen } from "@testing-library/react";
import React, { useLayoutEffect, useState } from "react";

import { LayoutGroup, useLayoutGroupEffect } from "../LayoutGroup.js";
import { useLayoutGroupEffect } from "../../hooks/useLayoutGroupEffect.js";
import { LayoutGroup } from "../LayoutGroup.js";

describe("DeferredLayoutEffects", () => {
describe("LayoutGroup", () => {
jest.useFakeTimers("modern");

it("registers multiple effects and runs them", () => {
Expand Down
115 changes: 0 additions & 115 deletions src/contexts/LayoutGroup.tsx

This file was deleted.

4 changes: 2 additions & 2 deletions src/hooks/__tests__/useEditorViewLayoutEffect.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import type { EditorState } from "prosemirror-state";
import type { EditorView } from "prosemirror-view";
import React from "react";

import { EditorContext } from "../../contexts/EditorContext.js";
import { LayoutGroup } from "../../contexts/LayoutGroup.js";
import { EditorContext } from "../../components/EditorContext.js";
import { LayoutGroup } from "../../components/LayoutGroup.js";
import { useEditorEffect } from "../useEditorEffect.js";

function TestComponent({
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useEditorEffect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type { EditorView } from "prosemirror-view";
import { useContext } from "react";
import type { DependencyList } from "react";

import { EditorContext } from "../contexts/EditorContext.js";
import { useLayoutGroupEffect } from "../contexts/LayoutGroup.js";
import { EditorContext } from "../components/EditorContext.js";
import { useLayoutGroupEffect } from "../hooks/useLayoutGroupEffect.js";

/**
* Registers a layout effect to run after the EditorView has
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useEditorEventCallback.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { EditorView } from "prosemirror-view";
import { useCallback, useContext, useRef } from "react";

import { EditorContext } from "../contexts/EditorContext.js";
import { EditorContext } from "../components/EditorContext.js";

import { useEditorEffect } from "./useEditorEffect.js";

Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useEditorEventListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Plugin } from "prosemirror-state";
import type { DOMEventMap, EditorView } from "prosemirror-view";
import { useCallback, useContext, useRef } from "react";

import { EditorContext } from "../contexts/EditorContext.js";
import { EditorContext } from "../components/EditorContext.js";
import type { EventHandler } from "../plugins/componentEventListeners.js";

import { useEditorEffect } from "./useEditorEffect.js";
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useEditorState.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { EditorState } from "prosemirror-state";
import { useContext } from "react";

import { EditorContext } from "../contexts/EditorContext.js";
import { EditorContext } from "../components/EditorContext.js";

/**
* Provides access to the current EditorState value.
Expand Down
Loading
Loading