Skip to content

Commit

Permalink
Minimize forks of prosemirror-view
Browse files Browse the repository at this point in the history
  • Loading branch information
smoores-dev committed Aug 14, 2023
1 parent 6525c86 commit 7f4a754
Show file tree
Hide file tree
Showing 33 changed files with 3,525 additions and 198 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
.yarnrc.yml
.pnp.*

src/prosemirror-internal/
src/prosemirror-view/
4 changes: 2 additions & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
.yarnrc.yml
.pnp.*

src/prosemirror-internal/*
!src/prosemirror-internal/README.md
src/prosemirror-view/*
!src/prosemirror-view/README.md
2 changes: 1 addition & 1 deletion src/components/DocNodeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import React, {
import { ChildDescriptorsContext } from "../contexts/ChildDescriptorsContext.js";
import { useChildNodeViews } from "../hooks/useChildNodeViews.js";
import { useNodeViewDescriptor } from "../hooks/useNodeViewDescriptor.js";
import { DecorationSourceInternal } from "../prosemirror-internal/DecorationInternal.js";
import { DecorationSourceInternal } from "../prosemirror-view/DecorationInternal.js";

type Props = {
node: Node;
Expand Down
52 changes: 11 additions & 41 deletions src/components/EditorView.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import { Command, EditorState, Transaction } from "prosemirror-state";
import {
DecorationSet,
DirectEditorProps,
EditorView as EditorViewT,
} from "prosemirror-view";
import React, {
DetailedHTMLProps,
ForwardRefExoticComponent,
Expand All @@ -20,41 +15,16 @@ import { NodeViewContext } from "../contexts/NodeViewContext.js";
import { useContentEditable } from "../hooks/useContentEditable.js";
import { useSyncSelection } from "../hooks/useSyncSelection.js";
import { usePluginViews } from "../hooks/useViewPlugins.js";
import { DecorationSourceInternal } from "../prosemirror-internal/DecorationInternal.js";
import { EditorViewInternal } from "../prosemirror-internal/EditorViewInternal.js";
import { DOMObserver } from "../prosemirror-internal/domobserver.js";
import { initInput } from "../prosemirror-internal/input.js";
import { DecorationSourceInternal } from "../prosemirror-view/DecorationInternal.js";
import {
DecorationSet,
DirectEditorProps,
EditorView as EditorViewClass,
} from "../prosemirror-view/index.js";

import { DocNodeView } from "./DocNodeView.js";
import { NodeViewComponentProps } from "./NodeViewComponentProps.js";

class ReactEditorView extends EditorViewT {
constructor(place: { mount: HTMLElement }, props: DirectEditorProps) {
super(null, props);
this.dom = place.mount;
this.domObserver.stop();
this.domObserver = new DOMObserver(this as unknown as EditorViewInternal);
this.domObserver.start();
initInput(this);
}
set docView(_docView) {
// We handle this ourselves
}
get docView() {
return this.dom.pmViewDesc;
}
updateState(state: EditorState) {
// eslint-disable-next-line react/no-direct-mutation-state
this.state = state;
}
update(_props: DirectEditorProps) {
// React takes care of this
}
destroy() {
// React takes care of this
}
}

type EditorStateProps =
| {
state: EditorState;
Expand All @@ -77,7 +47,7 @@ export type EditorProps = Omit<
>;
};
decorations?: DecorationSet;
dispatchTransaction?: (this: EditorViewT, tr: Transaction) => void;
dispatchTransaction?: (this: EditorViewClass, tr: Transaction) => void;
};

export type Props = EditorProps &
Expand Down Expand Up @@ -125,7 +95,7 @@ export function EditorView(props: Props) {
// This is only safe to use in effects/layout effects or
// event handlers!
const [reactEditorView, setReactEditorView] =
useState<EditorViewInternal | null>(null);
useState<EditorViewClass | null>(null);
reactEditorView?.updateState(state);

useEffect(() => {
Expand Down Expand Up @@ -153,7 +123,7 @@ export function EditorView(props: Props) {
return;
}
if (el && !reactEditorView) {
const newReactEditorView = new ReactEditorView(
const newReactEditorView = new EditorViewClass(
{ mount: el },
{
decorations: getDecorations,
Expand All @@ -172,7 +142,7 @@ export function EditorView(props: Props) {
handleTripleClickOn,
editable: () => editable,
state: EditorState.create({ schema: state.schema }),
dispatchTransaction(this: EditorViewT, tr) {
dispatchTransaction(this: EditorViewClass, tr) {
if (dispatchProp) {
dispatchProp.call(this, tr);
} else {
Expand All @@ -181,7 +151,7 @@ export function EditorView(props: Props) {
},
plugins,
}
) as unknown as EditorViewInternal;
);

newReactEditorView.updateState(state);

Expand Down
2 changes: 1 addition & 1 deletion src/components/MarkView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import React, {
} from "react";

import { ChildDescriptorsContext } from "../contexts/ChildDescriptorsContext.js";
import { MarkViewDesc, ViewDesc } from "../descriptors/ViewDesc.js";
import { MarkViewDesc, ViewDesc } from "../prosemirror-view/viewdesc.js";

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

Expand Down
2 changes: 1 addition & 1 deletion src/components/NodeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { useChildNodeViews } from "../hooks/useChildNodeViews.js";
import {
DecorationInternal,
DecorationSourceInternal,
} from "../prosemirror-internal/DecorationInternal.js";
} from "../prosemirror-view/DecorationInternal.js";

import { NodeViewComponentProps } from "./NodeViewComponentProps.js";
import { OutputSpec } from "./OutputSpec.js";
Expand Down
6 changes: 4 additions & 2 deletions src/components/TextNodeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { DecorationSet } from "prosemirror-view";
import { Component } from "react";
import { findDOMNode } from "react-dom";

import { TextViewDesc, ViewDesc } from "../descriptors/ViewDesc.js";
import { DecorationInternal } from "../prosemirror-internal/DecorationInternal.js";
import { DecorationInternal } from "../prosemirror-view/DecorationInternal.js";
import { TextViewDesc, ViewDesc } from "../prosemirror-view/viewdesc.js";

type Props = {
node: Node;
Expand All @@ -26,6 +26,7 @@ export class TextNodeView extends Component<Props> {

const desc = new TextViewDesc(
undefined,
[],
this.props.node,
[],
DecorationSet.empty,

Check failure on line 32 in src/components/TextNodeView.tsx

View workflow job for this annotation

GitHub Actions / check

Argument of type 'DecorationSet' is not assignable to parameter of type 'DecorationSource'.
Expand All @@ -48,6 +49,7 @@ export class TextNodeView extends Component<Props> {

const desc = new TextViewDesc(
undefined,
[],
this.props.node,
[],
DecorationSet.empty,

Check failure on line 55 in src/components/TextNodeView.tsx

View workflow job for this annotation

GitHub Actions / check

Argument of type 'DecorationSet' is not assignable to parameter of type 'DecorationSource'.
Expand Down
2 changes: 1 addition & 1 deletion src/components/TrailingHackView.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useContext, useLayoutEffect, useRef } from "react";

import { ChildDescriptorsContext } from "../contexts/ChildDescriptorsContext.js";
import { TrailingHackViewDesc } from "../descriptors/ViewDesc.js";
import { TrailingHackViewDesc } from "../prosemirror-view/viewdesc.js";

export function TrailingHackView() {
const siblingDescriptors = useContext(ChildDescriptorsContext);
Expand Down
4 changes: 2 additions & 2 deletions src/components/WidgetView.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, { useContext, useLayoutEffect, useRef } from "react";

import { ChildDescriptorsContext } from "../contexts/ChildDescriptorsContext.js";
import { WidgetViewDesc } from "../descriptors/ViewDesc.js";
import { ReactWidgetDecoration } from "../prosemirror-internal/DecorationInternal.js";
import { ReactWidgetDecoration } from "../prosemirror-view/DecorationInternal.js";
import { WidgetViewDesc } from "../prosemirror-view/viewdesc.js";

type Props = {
widget: ReactWidgetDecoration;
Expand Down
2 changes: 1 addition & 1 deletion src/contexts/ChildDescriptorsContext.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createContext } from "react";

import { ViewDesc } from "../descriptors/ViewDesc.js";
import { ViewDesc } from "../prosemirror-view/viewdesc.js";

export const ChildDescriptorsContext = createContext<ViewDesc[]>([]);
6 changes: 2 additions & 4 deletions src/contexts/EditorViewContext.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { createContext } from "react";

import { EditorViewInternal } from "../prosemirror-internal/EditorViewInternal.js";
import { EditorView } from "../prosemirror-view/index.js";

export const EditorViewContext = createContext(
null as unknown as EditorViewInternal
);
export const EditorViewContext = createContext<EditorView | null>(null);
2 changes: 1 addition & 1 deletion src/decorations/DecorationType.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Mappable } from "prosemirror-transform";
import { Decoration } from "prosemirror-view";

import { DOMNode } from "../prosemirror-internal/dom.js";
import { DOMNode } from "../prosemirror-view/dom.js";

export interface DecorationType {
spec: unknown;
Expand Down
6 changes: 3 additions & 3 deletions src/descriptors/ViewDesc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import {
DecorationInternal,
DecorationSourceInternal,
ReactWidgetDecoration,
} from "../prosemirror-internal/DecorationInternal.js";
import * as browser from "../prosemirror-internal/browser.js";
} from "../prosemirror-view/DecorationInternal.js";
import * as browser from "../prosemirror-view/browser.js";
import {
DOMNode,
domIndex,
isEquivalentPosition,
} from "../prosemirror-internal/dom.js";
} from "../prosemirror-view/dom.js";

export function sameOuterDeco(
a: readonly DecorationInternal[],
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useChildNodeViews.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
DecorationSourceInternal,
NonWidgetType,
ReactWidgetDecoration,
} from "../prosemirror-internal/DecorationInternal.js";
} from "../prosemirror-view/DecorationInternal.js";

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

Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useContentEditable.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect } from "react";

import { EditorViewInternal } from "../prosemirror-internal/EditorViewInternal.js";
import { EditorViewInternal } from "../prosemirror-view/EditorViewInternal.js";

Check failure on line 3 in src/hooks/useContentEditable.ts

View workflow job for this annotation

GitHub Actions / check

Cannot find module '../prosemirror-view/EditorViewInternal.js' or its corresponding type declarations.

export function useContentEditable(view: EditorViewInternal | null) {
useEffect(() => {
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useNodeViewDescriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { Node } from "prosemirror-model";
import { MutableRefObject, useContext, useLayoutEffect } from "react";

import { ChildDescriptorsContext } from "../contexts/ChildDescriptorsContext.js";
import { NodeViewDesc, ViewDesc } from "../descriptors/ViewDesc.js";
import {
DecorationInternal,
DecorationSourceInternal,
} from "../prosemirror-internal/DecorationInternal.js";
} from "../prosemirror-view/DecorationInternal.js";
import { NodeViewDesc, ViewDesc } from "../prosemirror-view/viewdesc.js";

export function useNodeViewDescriptor(
node: Node,
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useSyncSelection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEffect } from "react";

import { EditorViewInternal } from "../prosemirror-internal/EditorViewInternal.js";
import { selectionToDOM } from "../prosemirror-internal/selection.js";
import { EditorViewInternal } from "../prosemirror-view/EditorViewInternal.js";

Check failure on line 3 in src/hooks/useSyncSelection.ts

View workflow job for this annotation

GitHub Actions / check

Cannot find module '../prosemirror-view/EditorViewInternal.js' or its corresponding type declarations.
import { selectionToDOM } from "../prosemirror-view/selection.js";

export function useSyncSelection(view: EditorViewInternal | null) {
useEffect(() => {
Expand Down
5 changes: 3 additions & 2 deletions src/hooks/useView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { EditorViewContext } from "../contexts/EditorViewContext.js";
import { useLayoutGroupEffect } from "../contexts/LayoutGroup.js";

export function useView(effect: (view: EditorViewT) => void) {
const viewApi = useContext(EditorViewContext);
const view = useContext(EditorViewContext);
useLayoutGroupEffect(() => {
return effect(viewApi);
if (!view) return;
return effect(view);

Check failure on line 11 in src/hooks/useView.ts

View workflow job for this annotation

GitHub Actions / check

Argument of type 'import("/home/runner/work/react-prosemirror/react-prosemirror/src/prosemirror-view/index").EditorView' is not assignable to parameter of type 'import("/home/runner/work/react-prosemirror/react-prosemirror/.yarn/cache/prosemirror-view-npm-1.30.1-f24ae246c7-6dec020595.zip/node_modules/prosemirror-view/dist/index").EditorView'.
});
}
2 changes: 1 addition & 1 deletion src/hooks/useViewPlugins.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Plugin, PluginView } from "prosemirror-state";
import { useLayoutEffect, useRef } from "react";

import { EditorViewInternal } from "../prosemirror-internal/EditorViewInternal.js";
import { EditorViewInternal } from "../prosemirror-view/EditorViewInternal.js";

Check failure on line 4 in src/hooks/useViewPlugins.ts

View workflow job for this annotation

GitHub Actions / check

Cannot find module '../prosemirror-view/EditorViewInternal.js' or its corresponding type declarations.

import { usePrevious } from "./usePrev.js";

Expand Down
41 changes: 0 additions & 41 deletions src/prosemirror-internal/EditorViewInternal.ts

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 7f4a754

Please sign in to comment.