Skip to content

Commit

Permalink
Merge branch 'react-editor-view' into next-built
Browse files Browse the repository at this point in the history
  • Loading branch information
smoores-dev committed Nov 13, 2024
2 parents 831bd66 + aa1dfee commit 2dcc825
Show file tree
Hide file tree
Showing 48 changed files with 1,939 additions and 937 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"rules": {
"no-console": ["error", { "allow": ["error"] }],
"react/prop-types": "off",
"react-hooks/exhaustive-deps": [
"warn",
{
Expand Down
3 changes: 0 additions & 3 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,5 @@ jobs:
- name: Install dependencies
run: yarn

- uses: browser-actions/setup-chrome@v1
- uses: browser-actions/setup-firefox@v1

- name: Test
run: yarn test
4 changes: 1 addition & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
"**/.yarn": true,
"**/.pnp.*": true
},
// "typescript.tsdk": ".yarn/sdks/typescript/lib",
"typescript.tsdk": "node_modules/typescript/lib",
"typescript.enablePromptUseWorkspaceTsdk": true,
// "prettier.prettierPath": ".yarn/sdks/prettier/index.js",
// "eslint.nodePath": ".yarn/sdks",
"editor.formatOnSave": true,
"[javascript][typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
Expand Down
36 changes: 30 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ yarn add @nytimes/react-prosemirror
- [`useEditorEventListener`](#useeditoreventlistener-1)
- [`useEditorEffect`](#useeditoreffect-1)
- [`NodeViewComponentProps`](#nodeviewcomponentprops)
- [`useStopEvent`](#usestopevent)
- [`useSelectNode`](#useselectnode)
- [`widget`](#widget)

<!-- tocstop -->
Expand Down Expand Up @@ -573,12 +575,13 @@ export function SelectionWidget() {

```tsx
type NodeViewComponentProps = {
decorations: readonly Decoration[];
innerDecorations: DecorationSource;
node: Node;
children?: ReactNode | ReactNode[];
isSelected: boolean;
pos: number;
nodeProps: {
decorations: readonly Decoration[];
innerDecorations: DecorationSource;
node: Node;
children?: ReactNode | ReactNode[];
getPos: () => number;
};
} & HTMLAttributes<HTMLElement>;
```

Expand All @@ -594,6 +597,27 @@ and should pass them through to their top-level DOM element.
In addition to accepting these props, all node view components _must_ forward
their ref to their top-level DOM element.

### `useStopEvent`

```tsx
type useStopEvent = (stopEvent: (view: EditorView, event: Event) => boolean): void
```
This hook can be used within a node view component to register a
[stopEvent handler](https://prosemirror.net/docs/ref/#view.NodeView.stopEvent).
Events for which this returns true are not handled by the editor.
### `useSelectNode`
```tsx
type useSelectNode = (selectNode: () => void, deselectNode?: () => void): void
```
This hook can be used within a node view component to register
[selectNode and deselectNode handlers](https://prosemirror.net/docs/ref/#view.NodeView.selectNode).
The selectNode handler will only be called when a NodeSelection is created whose
node is this one.
### `widget`
```tsx
Expand Down
42 changes: 25 additions & 17 deletions demo/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import React, {
Ref,
StrictMode,
forwardRef,
useCallback,
useMemo,
useState,
} from "react";
import { createRoot } from "react-dom/client";
Expand Down Expand Up @@ -196,7 +198,7 @@ const Footnote = forwardRef(function Footnote(
});

const TestWidget = forwardRef(function TestWidget(
{ widget, pos, ...props }: WidgetViewComponentProps,
{ widget, getPos, ...props }: WidgetViewComponentProps,
ref: ForwardedRef<HTMLSpanElement>
) {
return (
Expand Down Expand Up @@ -303,6 +305,25 @@ function DemoEditor() {
const [state, setState] = useState(editorState);
const [showReactNodeViews, setShowReactNodeViews] = useState(true);

const nodeViews = useMemo(
() =>
showReactNodeViews
? {
paragraph: Paragraph,
list: List,
list_item: ListItem,
footnote: Footnote,
}
: undefined,
[showReactNodeViews]
);

const dispatchTransaction = useCallback(function (tr) {
setState((prev) => {
return prev.apply(tr);
});
}, []);

return (
<main>
<h1>React ProseMirror Demo</h1>
Expand All @@ -324,25 +345,12 @@ function DemoEditor() {
key={`${showReactNodeViews}`}
className="ProseMirror"
state={state}
dispatchTransaction={function (tr) {
setState((prev) => {
return prev.apply(tr);
});
}}
dispatchTransaction={dispatchTransaction}
plugins={plugins}
nodeViews={
showReactNodeViews
? {
paragraph: Paragraph,
list: List,
list_item: ListItem,
footnote: Footnote,
}
: undefined
}
nodeViews={nodeViews}
customNodeViews={showReactNodeViews ? undefined : customNodeViews}
>
<ProseMirrorDoc as={<article />} />
<ProseMirrorDoc />
</ProseMirror>
</main>
);
Expand Down
53 changes: 0 additions & 53 deletions docs/assets/index-B1RmUjDz.js

This file was deleted.

53 changes: 53 additions & 0 deletions docs/assets/index-k0PL4xKq.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>React-ProseMirror Demo</title>
<script type="module" crossorigin src="/react-prosemirror/assets/index-B1RmUjDz.js"></script>
<script type="module" crossorigin src="/react-prosemirror/assets/index-k0PL4xKq.js"></script>
<link rel="stylesheet" crossorigin href="/react-prosemirror/assets/index-DAGU9WLy.css">
</head>
<body>
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nytimes/react-prosemirror",
"version": "0.7.0-next.9",
"version": "0.7.0-next.10",
"license": "Apache-2.0",
"type": "module",
"main": "dist/cjs/index.js",
Expand Down Expand Up @@ -57,7 +57,9 @@
"@vitejs/plugin-react": "^4.3.1",
"@wdio/browser-runner": "^9.0.9",
"@wdio/cli": "^9.0.9",
"@wdio/dot-reporter": "^9.1.0",
"@wdio/mocha-framework": "^9.0.8",
"@wdio/spec-reporter": "^9.1.0",
"@wdio/types": "^9.0.8",
"@yarnpkg/sdks": "^3.0.0-rc.38",
"concurrently": "^7.6.0",
Expand Down
Loading

0 comments on commit 2dcc825

Please sign in to comment.