浏览代码

impl launchHandsontableModal with EventEmitter

Yuki Takei 3 年之前
父节点
当前提交
3dfc6f1028

+ 4 - 0
packages/app/src/client/base.jsx

@@ -1,5 +1,7 @@
 import React from 'react';
 import React from 'react';
 
 
+import EventEmitter from 'events';
+
 import AppContainer from '~/client/services/AppContainer';
 import AppContainer from '~/client/services/AppContainer';
 import SocketIoContainer from '~/client/services/SocketIoContainer';
 import SocketIoContainer from '~/client/services/SocketIoContainer';
 import { DescendantsPageListModal } from '~/components/DescendantsPageListModal';
 import { DescendantsPageListModal } from '~/components/DescendantsPageListModal';
@@ -28,6 +30,8 @@ if (!window) {
 const xss = new Xss();
 const xss = new Xss();
 window.xss = xss;
 window.xss = xss;
 
 
+window.globalEmitter = new EventEmitter();
+
 // create unstated container instance
 // create unstated container instance
 const appContainer = new AppContainer();
 const appContainer = new AppContainer();
 // eslint-disable-next-line no-unused-vars
 // eslint-disable-next-line no-unused-vars

+ 0 - 11
packages/app/src/client/services/AppContainer.js

@@ -180,17 +180,6 @@ export default class AppContainer extends Container {
     return renderer;
     return renderer;
   }
   }
 
 
-
-  launchHandsontableModal(componentKind, beginLineNumber, endLineNumber) {
-    let targetComponent;
-    switch (componentKind) {
-      case 'page':
-        targetComponent = this.getComponentInstance('Page');
-        break;
-    }
-    targetComponent.launchHandsontableModal(beginLineNumber, endLineNumber);
-  }
-
   launchDrawioModal(componentKind, beginLineNumber, endLineNumber) {
   launchDrawioModal(componentKind, beginLineNumber, endLineNumber) {
     let targetComponent;
     let targetComponent;
     switch (componentKind) {
     switch (componentKind) {

+ 1 - 1
packages/app/src/client/util/markdown-it/table-with-handsontable-button.js

@@ -9,7 +9,7 @@ export default class TableWithHandsontableButtonConfigurer {
       const beginLine = tokens[idx].map[0] + 1;
       const beginLine = tokens[idx].map[0] + 1;
       const endLine = tokens[idx].map[1];
       const endLine = tokens[idx].map[1];
       // eslint-disable-next-line max-len
       // eslint-disable-next-line max-len
-      return `<div class="editable-with-handsontable"><button class="handsontable-modal-trigger" onClick="crowi.launchHandsontableModal('page', ${beginLine}, ${endLine})"><i class="icon-note"></i></button><table class="table table-bordered">`;
+      return `<div class="editable-with-handsontable"><button class="handsontable-modal-trigger" onClick="globalEmitter.emit('launchHandsontableModal', ${beginLine}, ${endLine})"><i class="icon-note"></i></button><table class="table table-bordered">`;
     };
     };
 
 
     md.renderer.rules.table_close = (tokens, idx) => {
     md.renderer.rules.table_close = (tokens, idx) => {

+ 17 - 1
packages/app/src/components/Page.jsx

@@ -1,4 +1,4 @@
-import React from 'react';
+import React, { useEffect, useRef } from 'react';
 
 
 import PropTypes from 'prop-types';
 import PropTypes from 'prop-types';
 
 
@@ -197,6 +197,21 @@ const PageWrapper = (props) => {
   const { data: grantGroupId } = useSelectedGrantGroupId();
   const { data: grantGroupId } = useSelectedGrantGroupId();
   const { data: grantGroupName } = useSelectedGrantGroupName();
   const { data: grantGroupName } = useSelectedGrantGroupName();
 
 
+  const pageRef = useRef(null);
+
+  useEffect(() => {
+    const handler = (beginLineNumber, endLineNumber) => {
+      if (pageRef?.current != null) {
+        pageRef.current.launchHandsontableModal(beginLineNumber, endLineNumber);
+      }
+    };
+    window.globalEmitter.on('launchHandsontableModal', handler);
+
+    return function cleanup() {
+      window.globalEmitter.removeListener('launchHandsontableModal', handler);
+    };
+  });
+
   if (currentPagePath == null || editorMode == null || isGuestUser == null) {
   if (currentPagePath == null || editorMode == null || isGuestUser == null) {
     return null;
     return null;
   }
   }
@@ -204,6 +219,7 @@ const PageWrapper = (props) => {
   return (
   return (
     <Page
     <Page
       {...props}
       {...props}
+      ref={pageRef}
       pagePath={currentPagePath}
       pagePath={currentPagePath}
       editorMode={editorMode}
       editorMode={editorMode}
       isGuestUser={isGuestUser}
       isGuestUser={isGuestUser}