Skip to content
Snippets Groups Projects
Commit 386c8fe1 authored by chris's avatar chris
Browse files

only rerender editor on certain props

parent 2b979f7f
No related branches found
No related tags found
1 merge request!240Inline custom tags
...@@ -52,11 +52,6 @@ const Editoria = () => { ...@@ -52,11 +52,6 @@ const Editoria = () => {
key = 'editoriaMobile'; key = 'editoriaMobile';
} }
console.log(JSON.stringify('["test"]'));
// console.log(
// JSON.parse('[{"item":"custom-tag-label-1"}]'),
// );
const EditoriaComponent = useMemo( const EditoriaComponent = useMemo(
() => ( () => (
<> <>
...@@ -71,7 +66,7 @@ const Editoria = () => { ...@@ -71,7 +66,7 @@ const Editoria = () => {
// value={demo} // value={demo}
// readonly // readonly
layout={layout} layout={layout}
onChange={source => console.log(source)} // onChange={source => console.log(source)}
user={user} user={user}
/> />
</> </>
......
import React, { useMemo, useState, useContext } from 'react'; import React, { useMemo, useState, useContext } from 'react';
import { WaxContext } from 'wax-prosemirror-core'; import { WaxContext } from 'wax-prosemirror-core';
import styled from 'styled-components';
import MenuButton from '../../ui/buttons/MenuButton'; import MenuButton from '../../ui/buttons/MenuButton';
const StyledButton = styled(MenuButton)`
svg {
height: 15px;
width: 17px;
}
`;
const CustomTagInlineComponent = ({ view: { state }, item }) => { const CustomTagInlineComponent = ({ view: { state }, item }) => {
const { icon, title } = item; const { icon, title } = item;
const localInline = JSON.parse(localStorage.getItem('isInline')); const localInline = JSON.parse(localStorage.getItem('isInline'));
...@@ -27,7 +35,7 @@ const CustomTagInlineComponent = ({ view: { state }, item }) => { ...@@ -27,7 +35,7 @@ const CustomTagInlineComponent = ({ view: { state }, item }) => {
return useMemo( return useMemo(
() => ( () => (
<MenuButton <StyledButton
active={isOpen} active={isOpen}
disabled={isDisabled} disabled={isDisabled}
iconName={icon} iconName={icon}
...@@ -35,7 +43,7 @@ const CustomTagInlineComponent = ({ view: { state }, item }) => { ...@@ -35,7 +43,7 @@ const CustomTagInlineComponent = ({ view: { state }, item }) => {
title={title} title={title}
/> />
), ),
[isOpen], [isOpen, isDisabled],
); );
}; };
......
...@@ -76,9 +76,13 @@ const dropDownOptions = [ ...@@ -76,9 +76,13 @@ const dropDownOptions = [
]; ];
const EditingSuggesting = ({ view: { dispatch, state }, item }) => { const EditingSuggesting = ({ view: { dispatch, state }, item }) => {
const { app, activeView } = useContext(WaxContext); const { app, activeView, view } = useContext(WaxContext);
const isDisabled = app.config.get('config.EnableTrackChangeService').toggle; const isDisabled = app.config.get('config.EnableTrackChangeService').toggle;
const isEditable = view.main.props.editable(editable => {
return editable;
});
const enableDisableTrackChanges = () => { const enableDisableTrackChanges = () => {
app.config.get('config.EnableTrackChangeService').enabled = !app.config.get( app.config.get('config.EnableTrackChangeService').enabled = !app.config.get(
'config.EnableTrackChangeService', 'config.EnableTrackChangeService',
...@@ -109,7 +113,7 @@ const EditingSuggesting = ({ view: { dispatch, state }, item }) => { ...@@ -109,7 +113,7 @@ const EditingSuggesting = ({ view: { dispatch, state }, item }) => {
[], [],
); );
if (app.config.get('readonly')) return <Viewing />; if (!isEditable) return <Viewing />;
return EditingSuggestingComponent; return EditingSuggestingComponent;
}; };
......
...@@ -69,7 +69,7 @@ const FindAndReplaceTool = ({ view = {}, item }) => { ...@@ -69,7 +69,7 @@ const FindAndReplaceTool = ({ view = {}, item }) => {
/> />
{isOpen && ( {isOpen && (
<DropWrapper style={style} ref={dropElement}> <DropWrapper ref={dropElement} style={style}>
<FindAndReplaceComponent <FindAndReplaceComponent
close={() => { close={() => {
setIsOpen(false); setIsOpen(false);
...@@ -79,7 +79,7 @@ const FindAndReplaceTool = ({ view = {}, item }) => { ...@@ -79,7 +79,7 @@ const FindAndReplaceTool = ({ view = {}, item }) => {
)} )}
</Wrapper> </Wrapper>
), ),
[isOpen, style], [isOpen, style, isDisabled],
); );
return MemorizedDropdown; return MemorizedDropdown;
......
...@@ -62,7 +62,7 @@ const SpecialCharactersTool = ({ view = {}, item }) => { ...@@ -62,7 +62,7 @@ const SpecialCharactersTool = ({ view = {}, item }) => {
)} )}
</Wrapper> </Wrapper>
), ),
[isOpen], [isOpen, isDisabled],
); );
return MemorizedDropdown; return MemorizedDropdown;
......
...@@ -108,7 +108,7 @@ const AcceptRejectAllControls = styled.div` ...@@ -108,7 +108,7 @@ const AcceptRejectAllControls = styled.div`
position: absolute; position: absolute;
right: 207px; right: 207px;
transform-origin: 50% 50% 0px; transform-origin: 50% 50% 0px;
width: 200px; width: 209px;
z-index: 9999; z-index: 9999;
`; `;
......
import React, { useRef, useContext, useCallback } from 'react'; import React, { useRef, useContext, useCallback, useMemo } from 'react';
import applyDevTools from 'prosemirror-dev-tools'; import applyDevTools from 'prosemirror-dev-tools';
import { EditorState } from 'prosemirror-state'; import { EditorState } from 'prosemirror-state';
...@@ -12,60 +12,61 @@ import transformPasted from './helpers/TransformPasted'; ...@@ -12,60 +12,61 @@ import transformPasted from './helpers/TransformPasted';
export default props => { export default props => {
const { readonly, onBlur, options, debug, autoFocus, user } = props; const { readonly, onBlur, options, debug, autoFocus, user } = props;
const editorRef = useRef(); const editorRef = useRef();
let view; let view;
const context = useContext(WaxContext); const context = useContext(WaxContext);
const setEditorRef = useCallback(node => { const setEditorRef = useCallback(
if (editorRef.current) { node => {
// this is where you do cleanup if you have to. the editorRef.current will if (editorRef.current) {
// still point to the old ref, the old node. so you have some time here to // this is where you do cleanup if you have to. the editorRef.current will
// clean up the unmount if you need to. // still point to the old ref, the old node. so you have some time here to
} // clean up the unmount if you need to.
}
if (node) { if (node) {
view = new EditorView( view = new EditorView(
{ mount: node }, { mount: node },
{ {
editable: () => !readonly, editable: () => !readonly,
state: EditorState.create(options), state: EditorState.create(options),
dispatchTransaction, dispatchTransaction,
user, user,
scrollMargin: 200, scrollMargin: 200,
scrollThreshold: 200, scrollThreshold: 200,
handleDOMEvents: { handleDOMEvents: {
blur: onBlur blur: onBlur
? view => { ? view => {
onBlur(view.state.doc.content); onBlur(view.state.doc.content);
} }
: null, : null,
},
transformPasted: slice => {
return transformPasted(slice, view);
},
attributes: {
spellcheck: 'false',
},
}, },
transformPasted: slice => { );
return transformPasted(slice, view);
},
attributes: {
spellcheck: 'false',
},
},
);
context.updateView( context.updateView(
{ {
main: view, main: view,
}, },
'main', 'main',
); );
if (debug) applyDevTools(view); if (debug) applyDevTools(view);
if (autoFocus) if (autoFocus)
setTimeout(() => { setTimeout(() => {
view.focus(); view.focus();
}, 1000); }, 1000);
return () => view.destroy(); return () => view.destroy();
} }
editorRef.current = node; editorRef.current = node;
}, []); },
[readonly],
);
const dispatchTransaction = transaction => { const dispatchTransaction = transaction => {
const { TrackChange } = props; const { TrackChange } = props;
...@@ -94,7 +95,11 @@ export default props => { ...@@ -94,7 +95,11 @@ export default props => {
}; };
const editor = <div ref={setEditorRef} />; const editor = <div ref={setEditorRef} />;
return props.children({ return useMemo(
editor, () =>
}); props.children({
editor,
}),
[readonly],
);
}; };
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment