Yuki Takei 3 лет назад
Родитель
Сommit
4f76d27f36

+ 13 - 14
packages/app/src/components/PageEditor.tsx

@@ -31,11 +31,8 @@ import loggerFactory from '~/utils/logger';
 import Editor from './PageEditor/Editor';
 import Editor from './PageEditor/Editor';
 import Preview from './PageEditor/Preview';
 import Preview from './PageEditor/Preview';
 import scrollSyncHelper from './PageEditor/ScrollSyncHelper';
 import scrollSyncHelper from './PageEditor/ScrollSyncHelper';
-// import { withUnstatedContainers } from './UnstatedUtils';
 
 
 
 
-// TODO: remove this when omitting unstated is completed
-
 const logger = loggerFactory('growi:PageEditor');
 const logger = loggerFactory('growi:PageEditor');
 
 
 
 
@@ -394,19 +391,21 @@ const PageEditor = React.memo((props: Props): JSX.Element => {
     }
     }
   }, [editorMode]);
   }, [editorMode]);
 
 
+  // Unnecessary code. Delete after PageEditor and PageEditorByHackmd implementation has completed. -- 2022.09.06 Yuki Takei
+  //
   // set handler to update editor value
   // set handler to update editor value
-  useEffect(() => {
-    const handler = (markdown) => {
-      if (editorRef.current != null) {
-        editorRef.current.setValue(markdown);
-      }
-    };
-    globalEmitter.on('updateEditorValue', handler);
+  // useEffect(() => {
+  //   const handler = (markdown) => {
+  //     if (editorRef.current != null) {
+  //       editorRef.current.setValue(markdown);
+  //     }
+  //   };
+  //   globalEmitter.on('updateEditorValue', handler);
 
 
-    return function cleanup() {
-      globalEmitter.removeListener('updateEditorValue', handler);
-    };
-  }, []);
+  //   return function cleanup() {
+  //     globalEmitter.removeListener('updateEditorValue', handler);
+  //   };
+  // }, []);
 
 
   // Displays an alert if there is a difference with original markdown body
   // Displays an alert if there is a difference with original markdown body
   useEffect(() => {
   useEffect(() => {

+ 2 - 14
packages/app/src/components/PageEditor/CodeMirrorEditor.jsx

@@ -1,7 +1,7 @@
 import React from 'react';
 import React from 'react';
 
 
 import { createValidator } from '@growi/codemirror-textlint';
 import { createValidator } from '@growi/codemirror-textlint';
-import * as codemirror from 'codemirror';
+import { commands } from 'codemirror';
 import { JSHINT } from 'jshint';
 import { JSHINT } from 'jshint';
 import * as loadCssSync from 'load-css-file';
 import * as loadCssSync from 'load-css-file';
 import PropTypes from 'prop-types';
 import PropTypes from 'prop-types';
@@ -39,14 +39,6 @@ import styles from './CodeMirrorEditor.module.scss';
 window.JSHINT = JSHINT;
 window.JSHINT = JSHINT;
 window.kuromojin = { dicPath: '/static/dict' };
 window.kuromojin = { dicPath: '/static/dict' };
 
 
-// set save handler
-codemirror.commands.save = (instance) => {
-  if (instance.codeMirrorEditor != null) {
-    instance.codeMirrorEditor.dispatchSave();
-  }
-};
-// set CodeMirror instance as 'CodeMirror' so that CDN addons can reference
-window.CodeMirror = require('codemirror');
 require('codemirror/addon/display/placeholder');
 require('codemirror/addon/display/placeholder');
 require('codemirror/addon/edit/matchbrackets');
 require('codemirror/addon/edit/matchbrackets');
 require('codemirror/addon/edit/matchtags');
 require('codemirror/addon/edit/matchtags');
@@ -107,7 +99,6 @@ class CodeMirrorEditor extends AbstractEditor {
     this.logger = loggerFactory('growi:PageEditor:CodeMirrorEditor');
     this.logger = loggerFactory('growi:PageEditor:CodeMirrorEditor');
 
 
     this.state = {
     this.state = {
-      value: this.props.value,
       isGfmMode: this.props.isGfmMode,
       isGfmMode: this.props.isGfmMode,
       isLoadingKeymap: false,
       isLoadingKeymap: false,
       isSimpleCheatsheetShown: this.props.isGfmMode && this.props.value.length === 0,
       isSimpleCheatsheetShown: this.props.isGfmMode && this.props.value.length === 0,
@@ -252,7 +243,6 @@ class CodeMirrorEditor extends AbstractEditor {
    * @inheritDoc
    * @inheritDoc
    */
    */
   setValue(newValue) {
   setValue(newValue) {
-    this.setState({ value: newValue });
     this.getCodeMirror().getDoc().setValue(newValue);
     this.getCodeMirror().getDoc().setValue(newValue);
   }
   }
 
 
@@ -508,7 +498,7 @@ class CodeMirrorEditor extends AbstractEditor {
    */
    */
   handleEnterKey() {
   handleEnterKey() {
     if (!this.state.isGfmMode) {
     if (!this.state.isGfmMode) {
-      codemirror.commands.newlineAndIndent(this.getCodeMirror());
+      commands.newlineAndIndent(this.getCodeMirror());
       return;
       return;
     }
     }
 
 
@@ -1002,8 +992,6 @@ class CodeMirrorEditor extends AbstractEditor {
           //   editor.on('paste', this.pasteHandler);
           //   editor.on('paste', this.pasteHandler);
           //   editor.on('scrollCursorIntoView', this.scrollCursorIntoViewHandler);
           //   editor.on('scrollCursorIntoView', this.scrollCursorIntoViewHandler);
           // }}
           // }}
-          // temporary set props.value
-          // value={this.state.value}
           value={this.props.value}
           value={this.props.value}
           options={{
           options={{
             indentUnit: this.props.indentSize,
             indentUnit: this.props.indentSize,

+ 10 - 1
packages/app/src/components/UncontrolledCodeMirror.tsx

@@ -2,9 +2,18 @@ import React, {
   useCallback, useRef, MutableRefObject,
   useCallback, useRef, MutableRefObject,
 } from 'react';
 } from 'react';
 
 
-import { Editor } from 'codemirror';
+import { commands, Editor } from 'codemirror';
 import { ICodeMirror, UnControlled as CodeMirror } from 'react-codemirror2';
 import { ICodeMirror, UnControlled as CodeMirror } from 'react-codemirror2';
 
 
+// set save handler
+// CommandActions in @types/codemirror does not include 'save' but actualy exists
+// https://codemirror.net/5/doc/manual.html#commands
+(commands as any).save = (instance) => {
+  if (instance.codeMirrorEditor != null) {
+    instance.codeMirrorEditor.dispatchSave();
+  }
+};
+
 window.CodeMirror = require('codemirror');
 window.CodeMirror = require('codemirror');
 require('codemirror/addon/display/placeholder');
 require('codemirror/addon/display/placeholder');
 require('~/client/util/codemirror/gfm-growi.mode');
 require('~/client/util/codemirror/gfm-growi.mode');