Skip to content

Commit 51ce195

Browse files
committed
fix: use reducer
1 parent b81abae commit 51ce195

File tree

7 files changed

+67
-85
lines changed

7 files changed

+67
-85
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
import { createElement, ReactElement, useCallback } from "react";
2-
import { useFullscreen } from "../../utils/editorContext";
32
import { ToolbarButton } from "./ToolbarWrapper";
3+
import { CustomToolbarProps } from "./customToolbars";
44

5-
export function FullscreenButton(): ReactElement {
6-
const { isFullscreen, enterFullscreen, exitFullscreen } = useFullscreen();
7-
5+
export function FullscreenButton({ quill }: CustomToolbarProps): ReactElement {
86
const handleClick = useCallback(() => {
9-
if (isFullscreen) {
10-
exitFullscreen();
11-
} else {
12-
enterFullscreen();
13-
}
14-
}, [isFullscreen, enterFullscreen, exitFullscreen]);
7+
console.log("FullscreenButton handleClick", quill);
8+
quill?.emitter.emit("ACTION-DISPATCH", "SET_FULLSCREEN");
9+
}, [quill]);
1510

1611
return <ToolbarButton className={"icons icon-Expand"} title={"fullscreen"} onClick={handleClick} />;
1712
}

packages/pluggableWidgets/rich-text-web/src/components/CustomToolbars/toolbarHandlers.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { MutableRefObject } from "react";
44
import { Range } from "quill/core/selection";
55
import Keyboard, { Context } from "quill/modules/keyboard";
66
import { Scope } from "parchment";
7-
import { dispatchEditorAction } from "../../utils/editorContext";
87
/**
98
* give custom indent handler to use our custom "indent-left" and "indent-right" formats (formats/indent.ts)
109
*/
@@ -92,7 +91,6 @@ export function gotoStatusBarKeyboardHandler(this: Keyboard, _range: Range, cont
9291
* Keyboard handler for exiting fullscreen mode when the Escape key is pressed
9392
*/
9493
export function exitFullscreenKeyboardHandler(this: Keyboard, _range: Range, _context: Context): boolean | void {
95-
// Directly dispatch the EXIT_FULLSCREEN action to our EditorContext
96-
dispatchEditorAction({ type: "EXIT_FULLSCREEN" });
94+
this.quill.emitter.emit("ACTION-DISPATCH", "SET_FULLSCREEN", false);
9795
return true;
9896
}

packages/pluggableWidgets/rich-text-web/src/components/Editor.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
forwardRef,
77
Fragment,
88
MutableRefObject,
9+
useContext,
910
useEffect,
1011
useLayoutEffect,
1112
useRef
@@ -21,6 +22,7 @@ import {
2122
} from "./CustomToolbars/toolbarHandlers";
2223
import { useEmbedModal } from "./CustomToolbars/useEmbedModal";
2324
import Dialog from "./ModalDialog/Dialog";
25+
import { EditorDispatchContext } from "../store/EditorProvider";
2426

2527
export interface EditorProps {
2628
defaultValue?: string;
@@ -40,6 +42,7 @@ const Editor = forwardRef((props: EditorProps, ref: MutableRefObject<Quill | nul
4042
const modalRef = useRef<HTMLDivElement>(null);
4143
const onTextChangeRef = useRef(onTextChange);
4244
const onSelectionChangeRef = useRef(onSelectionChange);
45+
const contextDispatch = useContext(EditorDispatchContext);
4346

4447
const {
4548
showDialog,
@@ -131,6 +134,11 @@ const Editor = forwardRef((props: EditorProps, ref: MutableRefObject<Quill | nul
131134
quill.on("EDIT-TOOLTIP", (...arg: any[]) => {
132135
customLinkHandler(arg[0]);
133136
});
137+
quill.on("ACTION-DISPATCH", (type: string, ...arg: any[]) => {
138+
if (contextDispatch) {
139+
contextDispatch({ type, value: arg[0] });
140+
}
141+
});
134142
}
135143

136144
return () => {

packages/pluggableWidgets/rich-text-web/src/components/EditorWrapper.tsx

+15-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,19 @@ import classNames from "classnames";
55
import Quill, { Range } from "quill";
66
import "quill/dist/quill.core.css";
77
import "quill/dist/quill.snow.css";
8-
import { createElement, CSSProperties, ReactElement, useCallback, useEffect, useMemo, useRef, useState } from "react";
8+
import {
9+
createElement,
10+
CSSProperties,
11+
ReactElement,
12+
useCallback,
13+
useContext,
14+
useEffect,
15+
useMemo,
16+
useRef,
17+
useState
18+
} from "react";
919
import { RichTextContainerProps } from "typings/RichTextProps";
10-
import { EditorProvider, useFullscreen } from "../utils/editorContext";
20+
import { EditorContext, EditorProvider } from "../store/EditorProvider";
1121
import { updateLegacyQuillFormats } from "../utils/helpers";
1222
import MendixTheme from "../utils/themes/mxTheme";
1323
import { createPreset } from "./CustomToolbars/presets";
@@ -42,14 +52,16 @@ function EditorWrapperInner(props: EditorWrapperProps): ReactElement {
4252
tabIndex
4353
} = props;
4454

55+
const globalState = useContext(EditorContext);
56+
4557
const isFirstLoad = useRef<boolean>(false);
4658
const quillRef = useRef<Quill>(null);
4759
const [isFocus, setIsFocus] = useState(false);
4860
const editorValueRef = useRef<string>("");
4961
const toolbarRef = useRef<HTMLDivElement>(null);
5062
const [wordCount, setWordCount] = useState(0);
5163

52-
const { isFullscreen } = useFullscreen();
64+
const { isFullscreen } = globalState;
5365

5466
const [setAttributeValueDebounce] = useMemo(
5567
() =>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { createElement, createContext, ReactNode, useReducer } from "react";
2+
import { EditorAction, editorReducer, EditorState, initialState } from "./store";
3+
4+
export const EditorContext = createContext<EditorState>(initialState);
5+
export const EditorDispatchContext = createContext<React.Dispatch<EditorAction> | null>(null);
6+
7+
export function EditorProvider({ children }: { children: ReactNode }): JSX.Element {
8+
const [state, dispatch] = useReducer(editorReducer, initialState);
9+
10+
return (
11+
<EditorContext.Provider value={state}>
12+
<EditorDispatchContext.Provider value={dispatch}>{children}</EditorDispatchContext.Provider>
13+
</EditorContext.Provider>
14+
);
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export type EditorAction = { type: string; value: boolean };
2+
3+
export interface EditorState {
4+
isFullscreen: boolean;
5+
}
6+
7+
export const initialState: EditorState = {
8+
isFullscreen: false
9+
};
10+
11+
export function editorReducer(state: EditorState, action: EditorAction): EditorState {
12+
switch (action.type) {
13+
case "SET_FULLSCREEN":
14+
if (action.value === undefined || action.value === null) {
15+
return { ...state, isFullscreen: !state.isFullscreen };
16+
} else {
17+
return { ...state, isFullscreen: action.value };
18+
}
19+
20+
default:
21+
return state;
22+
}
23+
}

packages/pluggableWidgets/rich-text-web/src/utils/editorContext.tsx

-69
This file was deleted.

0 commit comments

Comments
 (0)