Просмотр исходного кода

Merge pull request #4268 from weseek/feat/7317-get-configs-from-db

get config from db
stevenfukase 4 лет назад
Родитель
Сommit
a7c6e158be

+ 6 - 3
packages/app/src/client/services/EditorContainer.js

@@ -15,6 +15,7 @@ export default class EditorContainer extends Container {
 
 
     this.appContainer = appContainer;
     this.appContainer = appContainer;
     this.appContainer.registerContainer(this);
     this.appContainer.registerContainer(this);
+    this.retrieveEditorSettings = this.retrieveEditorSettings.bind(this);
 
 
     const mainContent = document.querySelector('#content-main');
     const mainContent = document.querySelector('#content-main');
 
 
@@ -36,6 +37,7 @@ export default class EditorContainer extends Container {
       editorOptions: {},
       editorOptions: {},
       previewOptions: {},
       previewOptions: {},
       isTextlintEnabled: false,
       isTextlintEnabled: false,
+      textlintRules: [],
 
 
       indentSize: this.appContainer.config.adminPreferredIndentSize || 4,
       indentSize: this.appContainer.config.adminPreferredIndentSize || 4,
     };
     };
@@ -202,12 +204,13 @@ export default class EditorContainer extends Container {
    * Retrieve Editor Settings
    * Retrieve Editor Settings
    */
    */
   async retrieveEditorSettings() {
   async retrieveEditorSettings() {
-    const res = await this.appContainer.apiv3.get('/personal-setting');
-    const isTextlintEnabled = res.data.currentUser.editorCurrentSettings?.isTextlintEnabled;
+    const { data } = await this.appContainer.apiv3Get('/personal-setting/editor-settings');
+    const isTextlintEnabled = data?.textlintSettings?.isTextlintEnabled;
+    const textlintRules = data?.textlintSettings?.textlintRules;
     this.setState({
     this.setState({
       isTextlintEnabled,
       isTextlintEnabled,
+      textlintRules,
     });
     });
-    return isTextlintEnabled;
   }
   }
 
 
 }
 }

+ 8 - 7
packages/app/src/components/PageEditor/CodeMirrorEditor.jsx

@@ -158,10 +158,6 @@ export default class CodeMirrorEditor extends AbstractEditor {
       ? { dicPath: '/static/dict/cdn' }
       ? { dicPath: '/static/dict/cdn' }
       : { dicPath: 'https://cdn.jsdelivr.net/npm/kuromoji@0.1.2/dict' };
       : { dicPath: 'https://cdn.jsdelivr.net/npm/kuromoji@0.1.2/dict' };
 
 
-    // TODO: Get configs from db
-    this.isTextlintEnabled = true;
-    // this.textlintConfig = [];
-
     this.interceptorManager = new InterceptorManager();
     this.interceptorManager = new InterceptorManager();
     this.interceptorManager.addInterceptors([
     this.interceptorManager.addInterceptors([
       new PreventMarkdownListInterceptor(),
       new PreventMarkdownListInterceptor(),
@@ -204,8 +200,10 @@ export default class CodeMirrorEditor extends AbstractEditor {
     this.setKeymapMode(keymapMode);
     this.setKeymapMode(keymapMode);
   }
   }
 
 
-  initTextlintSettings() {
-    this.textlintValidator = createValidator(this.textlintConfig);
+  async initTextlintSettings() {
+    // If database has empty array, pass null instead to enable all default rules
+    const rulesForValidator = this.props.textlintRules?.length !== 0 ? this.props.textlintRules : null;
+    this.textlintValidator = createValidator(rulesForValidator);
     this.codemirrorLintConfig = { getAnnotations: this.textlintValidator, async: true };
     this.codemirrorLintConfig = { getAnnotations: this.textlintValidator, async: true };
   }
   }
 
 
@@ -984,12 +982,15 @@ export default class CodeMirrorEditor extends AbstractEditor {
 
 
 CodeMirrorEditor.propTypes = Object.assign({
 CodeMirrorEditor.propTypes = Object.assign({
   editorOptions: PropTypes.object.isRequired,
   editorOptions: PropTypes.object.isRequired,
-  isTextlintEnabled: PropTypes.bool.isRequired,
+  isTextlintEnabled: PropTypes.bool,
+  lintRules: PropTypes.array,
   emojiStrategy: PropTypes.object,
   emojiStrategy: PropTypes.object,
   lineNumbers: PropTypes.bool,
   lineNumbers: PropTypes.bool,
   onMarkdownHelpButtonClicked: PropTypes.func,
   onMarkdownHelpButtonClicked: PropTypes.func,
   onAddAttachmentButtonClicked: PropTypes.func,
   onAddAttachmentButtonClicked: PropTypes.func,
 }, AbstractEditor.propTypes);
 }, AbstractEditor.propTypes);
+
 CodeMirrorEditor.defaultProps = {
 CodeMirrorEditor.defaultProps = {
   lineNumbers: true,
   lineNumbers: true,
+  isTextlintEnabled: false,
 };
 };

+ 10 - 1
packages/app/src/components/PageEditor/Editor.jsx

@@ -10,6 +10,7 @@ import {
 import Dropzone from 'react-dropzone';
 import Dropzone from 'react-dropzone';
 
 
 import EditorContainer from '~/client/services/EditorContainer';
 import EditorContainer from '~/client/services/EditorContainer';
+import { withUnstatedContainers } from '../UnstatedUtils';
 
 
 import Cheatsheet from './Cheatsheet';
 import Cheatsheet from './Cheatsheet';
 import AbstractEditor from './AbstractEditor';
 import AbstractEditor from './AbstractEditor';
@@ -18,7 +19,7 @@ import TextAreaEditor from './TextAreaEditor';
 
 
 import pasteHelper from './PasteHelper';
 import pasteHelper from './PasteHelper';
 
 
-export default class Editor extends AbstractEditor {
+class Editor extends AbstractEditor {
 
 
   constructor(props) {
   constructor(props) {
     super(props);
     super(props);
@@ -46,6 +47,10 @@ export default class Editor extends AbstractEditor {
     this.renderDropzoneOverlay = this.renderDropzoneOverlay.bind(this);
     this.renderDropzoneOverlay = this.renderDropzoneOverlay.bind(this);
   }
   }
 
 
+  componentWillMount() {
+    this.props.editorContainer.retrieveEditorSettings();
+  }
+
   componentDidMount() {
   componentDidMount() {
     this.setState({ isComponentDidMount: true });
     this.setState({ isComponentDidMount: true });
   }
   }
@@ -317,6 +322,7 @@ export default class Editor extends AbstractEditor {
                         indentSize={editorContainer.state.indentSize}
                         indentSize={editorContainer.state.indentSize}
                         editorOptions={editorContainer.state.editorOptions}
                         editorOptions={editorContainer.state.editorOptions}
                         isTextlintEnabled={editorContainer.state.isTextlintEnabled}
                         isTextlintEnabled={editorContainer.state.isTextlintEnabled}
+                        textlintRules={editorContainer.state.textlintRules}
                         onPasteFiles={this.pasteFilesHandler}
                         onPasteFiles={this.pasteFilesHandler}
                         onDragEnter={this.dragEnterHandler}
                         onDragEnter={this.dragEnterHandler}
                         onMarkdownHelpButtonClicked={this.showMarkdownHelp}
                         onMarkdownHelpButtonClicked={this.showMarkdownHelp}
@@ -378,4 +384,7 @@ Editor.propTypes = Object.assign({
   emojiStrategy: PropTypes.object,
   emojiStrategy: PropTypes.object,
   onChange: PropTypes.func,
   onChange: PropTypes.func,
   onUpload: PropTypes.func,
   onUpload: PropTypes.func,
+  editorContainer: PropTypes.instanceOf(EditorContainer).isRequired,
 }, AbstractEditor.propTypes);
 }, AbstractEditor.propTypes);
+
+export default withUnstatedContainers(Editor, [EditorContainer]);

+ 3 - 2
packages/codemirror-textlint/src/index.ts

@@ -34,6 +34,7 @@ import { loggerFactory } from './utils/logger';
 type RulesConfigObj = {
 type RulesConfigObj = {
   name: string,
   name: string,
   options?: unknown,
   options?: unknown,
+  isEnabled?: boolean,
 }
 }
 
 
 type RuleExtension = {
 type RuleExtension = {
@@ -93,14 +94,14 @@ const createSetupRules = (rules, ruleOptions): TextlintKernelRule[] => (
 );
 );
 
 
 
 
-export const createValidator = (rulesConfigArray: RulesConfigObj[]): AsyncLinter<RulesConfigObj[]> => {
+export const createValidator = (rulesConfigArray: RulesConfigObj[] | null): AsyncLinter<RulesConfigObj[] | null> => {
   if (rulesConfigArray != null) {
   if (rulesConfigArray != null) {
     const filteredConfigArray = rulesConfigArray
     const filteredConfigArray = rulesConfigArray
       .filter((rule) => {
       .filter((rule) => {
         if (ruleModulesList[rule.name] == null) {
         if (ruleModulesList[rule.name] == null) {
           logger.error(`Textlint rule ${rule.name} is not installed`);
           logger.error(`Textlint rule ${rule.name} is not installed`);
         }
         }
-        return ruleModulesList[rule.name] != null;
+        return (ruleModulesList[rule.name] != null && rule.isEnabled !== false);
       });
       });
 
 
     const rules = filteredConfigArray
     const rules = filteredConfigArray