Skip to content

Commit

Permalink
Bug/scihub 1401 edit mode does not change (#91)
Browse files Browse the repository at this point in the history
* bug/SCIHUB-1401 migrate to new milkdown way of handling editable flag

* bug/SCIHUB-1401 updated changelog

* bug/SCIHUB-1401 remove type from useRef

---------

Co-authored-by: Krzysztof Nofz <[email protected]>
  • Loading branch information
zgrybus and Krzysztof Nofz authored Mar 18, 2024
1 parent 2d60ffb commit 4308895
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 41 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to this project will be documented in this file.

## [v0.3.1]

## Fixed
- After changing prop mode, it was still possible to write in the editor even though it was disabled.

## [v0.3.0]

## Added
Expand Down
18 changes: 13 additions & 5 deletions src/lib/packages/EditorContext/EditorContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ import {
defaultValueCtx,
editorViewOptionsCtx,
} from '@milkdown/core';
import { Ctx } from '@milkdown/ctx';
import { clipboard } from '@milkdown/plugin-clipboard';
import { emoji } from '@milkdown/plugin-emoji';
import { history } from '@milkdown/plugin-history';
import { trailing } from '@milkdown/plugin-trailing';
import { useEditor, UseEditorReturn } from '@milkdown/react';
import { createContext, useMemo } from 'react';
import { createContext, useEffect, useMemo, useRef } from 'react';

import { useCommonmarkPlugin } from './hooks/useCommonmarkPlugin/useCommonmarkPlugin';
import { useEditorViewPlugin } from './hooks/useEditorViewPlugin';
import { useGfmPlugin } from './hooks/useGfmPlugin/useGfmPlugin';
import { useListenerPlugin } from './hooks/useListenerPlugin';
import { useMathPlugin } from './hooks/useMathPlugin';
Expand All @@ -24,6 +22,7 @@ import { usePlaceholderPlugin } from './hooks/usePlaceholderPlugin';
import { usePrismPlugin } from './hooks/usePrismPlugin';
import { useSlashPlugin } from './hooks/useSlashPlugin';
import { useUploadPlugin } from './hooks/useUploadPlugin/useUploadPlugin';
import { useTextEditorContext } from '../../components/TextEditorContext/useTextEditorContext';

type EditorContextData = {
editor: UseEditorReturn | null;
Expand All @@ -48,6 +47,9 @@ export const EditorContextProvider: React.FC<EditorContextProviderProps> = ({
debounceChange,
defaultMarkdownValue,
}) => {
const { mode } = useTextEditorContext();
const isEditable = useRef(mode === 'active');

const gfmPlugin = useGfmPlugin();
const mathPlugin = useMathPlugin();
const uploadPlugin = useUploadPlugin();
Expand All @@ -64,14 +66,16 @@ export const EditorContextProvider: React.FC<EditorContextProviderProps> = ({
debounceChange,
});

useEditorViewPlugin();

const editor = useEditor(
root =>
MilkdownEditor.make()
.config(ctx => {
ctx.set(rootCtx, root);
ctx.set(defaultValueCtx, defaultMarkdownValue);
ctx.update(editorViewOptionsCtx, prev => ({
...prev,
editable: () => isEditable.current,
}));
})
.use(commonmarkPlugin)
.use(placeholderPlugin)
Expand Down Expand Up @@ -104,6 +108,10 @@ export const EditorContextProvider: React.FC<EditorContextProviderProps> = ({
]
);

useEffect(() => {
isEditable.current = mode === 'active';
}, [isEditable, mode]);

const context = useMemo(() => ({ editor }), [editor]);

return (
Expand Down
36 changes: 0 additions & 36 deletions src/lib/packages/EditorContext/hooks/useEditorViewPlugin.tsx

This file was deleted.

0 comments on commit 4308895

Please sign in to comment.