diff --git a/wax-prosemirror-core/index.js b/wax-prosemirror-core/index.js index 87baa280e2c2b9c63879649e67ba05a193f1f46f..8326713628d009b20ba80894519d014bdfee6c53 100644 --- a/wax-prosemirror-core/index.js +++ b/wax-prosemirror-core/index.js @@ -2,4 +2,10 @@ export { default as Service } from './src/Service'; export { WaxContext, useInjection } from './src/WaxContext'; export { PortalContext } from './src/PortalContext'; export { default as ComponentPlugin } from './src/ComponentPlugin'; + +/* UTILITIES */ +export { default as Middleware } from './src/utilities/lib/Middleware'; +// export { default as ToolGroup } from './src/utilities/lib/ToolGroup'; +// export { default as Tools } from './src/utilities/lib/Tools'; + export { default as Wax } from './src/Wax'; diff --git a/wax-prosemirror-services/src/lib/Middleware.js b/wax-prosemirror-core/src/utilities/lib/Middleware.js similarity index 87% rename from wax-prosemirror-services/src/lib/Middleware.js rename to wax-prosemirror-core/src/utilities/lib/Middleware.js index 25e8831120deacbd087d9e3eb371bf7a0f743c33..7bec012936a00d26e8e74266b21cb75c6f443137 100644 --- a/wax-prosemirror-services/src/lib/Middleware.js +++ b/wax-prosemirror-core/src/utilities/lib/Middleware.js @@ -2,14 +2,14 @@ export default class Middleware { constructor() { // Array prototype last if (!Array.prototype.last) { - Array.prototype.last = function() { + Array.prototype.last = function () { return this[this.length - 1]; }; } // Array prototype reduceOneRight if (!Array.prototype.reduceOneRight) { - Array.prototype.reduceOneRight = function() { + Array.prototype.reduceOneRight = function () { return this.slice(0, -1); }; } @@ -21,7 +21,7 @@ export default class Middleware { let _next = args.last(); fn.apply(this, [ ...args.reduceOneRight(), - _next.bind.apply(_next, [null, ...args.reduceOneRight()]) + _next.bind.apply(_next, [null, ...args.reduceOneRight()]), ]); }))(this.go); } diff --git a/wax-prosemirror-core/src/utilities/lib/ToolGroup.js b/wax-prosemirror-core/src/utilities/lib/ToolGroup.js new file mode 100644 index 0000000000000000000000000000000000000000..516dc77ab5d5c7802b1165ec612695243d6c9b4c --- /dev/null +++ b/wax-prosemirror-core/src/utilities/lib/ToolGroup.js @@ -0,0 +1,85 @@ +/* eslint-disable no-underscore-dangle */ +import React, { useMemo } from 'react'; +import { injectable, inject } from 'inversify'; +import { ToolGroupComponent, ToolGroups } from 'wax-prosemirror-components'; +import { v4 as uuidv4 } from 'uuid'; +import { isEmpty } from 'lodash'; + +class ToolGroup { + _config = {}; + title = ''; + _tools = []; + _toolGroups = []; + + setGroupConfig(config) { + this._config = config; + } + + set toolGroups(groups) { + this._toolGroups = groups; + } + + excludeIncludeTools() { + const { exclude = [], include = [] } = this._config; + + if (include.length > 0) { + this._tools.map(tool => { + if (include.includes(tool.name)) { + tool.displayTool(); + } else { + tool.hideTool(); + } + return true; + }); + } else { + this._tools.map(tool => { + if (exclude.includes(tool.name)) { + tool.hideTool(); + } + }); + } + } + + addToolIntoMore() { + const { more = [] } = this._config; + if (more.length > 0) { + this._tools.map(tool => { + if (more.includes(tool.name)) { + tool.hideInToolGroup(); + } else { + tool.displayInToolGroup(); + } + }); + } + } + + set tools(tools) { + this._tools = tools; + } + + renderTools(view) { + if (isEmpty(view)) return null; + + const { name } = this.constructor; + if (this._toolGroups > 0) { + return <ToolGroups toolGroups={this._toolGroups} view={view} />; + } + + const MemorizedToolGroupComponent = useMemo( + () => ( + <ToolGroupComponent + key={uuidv4()} + name={name} + title={this.title} + tools={this._tools} + view={view} + /> + ), + [], + ); + + return MemorizedToolGroupComponent; + } +} + +export default ToolGroup; diff --git a/wax-prosemirror-core/src/utilities/lib/Tools.js b/wax-prosemirror-core/src/utilities/lib/Tools.js new file mode 100644 index 0000000000000000000000000000000000000000..f6a92e4883432a844e3380e150f7f41d5611eabf --- /dev/null +++ b/wax-prosemirror-core/src/utilities/lib/Tools.js @@ -0,0 +1,86 @@ +/* eslint-disable */ +import React from 'react'; +import { v4 as uuidv4 } from 'uuid'; +import { isEmpty } from 'lodash'; +import { injectable, inject } from 'inversify'; +import { Button } from 'wax-prosemirror-components'; + +@injectable() +class Tools { + title = 'title'; + _isDisplayed = true; + _isHiddenInToolGroup = false; + config = {}; + pmplugins = {}; + name = 'name'; + constructor(@inject('Config') config, @inject('PmPlugins') pmplugins) { + this.config = config; + this.pmplugins = pmplugins; + } + + static get id() { + return this.name; + } + + get run() { + return true; + } + + get enable() { + return () => true; + } + + select() { + return () => true; + } + + get active() { + return () => false; + } + + toJSON() { + return { + title: this.title, + icon: this.icon, + label: this.label, + active: this.active, + run: this.run, + enable: this.enable, + select: this.select, + id: this.id, + }; + } + + renderTool(view) { + if (isEmpty(view)) return null; + + return this._isDisplayed ? ( + <Button key={uuidv4()} item={this.toJSON()} view={view} /> + ) : null; + } + + displayTool() { + this._isDisplayed = true; + } + + hideTool() { + this._isDisplayed = false; + } + + isDisplayed() { + return this._isDisplayed; + } + + displayInToolGroup() { + this._isHiddenInToolGroup = false; + } + + hideInToolGroup() { + this._isHiddenInToolGroup = true; + } + + isIntoMoreSection() { + return this._isHiddenInToolGroup; + } +} +export default Tools; diff --git a/wax-prosemirror-services/index.js b/wax-prosemirror-services/index.js index 735ab24bb716ab12d2951a087aab87984e0b44e5..2471d12025c8ea8a2aaa369c4626690bde9addae 100644 --- a/wax-prosemirror-services/index.js +++ b/wax-prosemirror-services/index.js @@ -1,21 +1,21 @@ +/* Base Services move to core */ export { default as LayoutService } from './src/LayoutService/LayoutService'; export { default as PortalService } from './src/PortalService/PortalService'; export { default as MenuService } from './src/MenuService/MenuService'; export { default as OverlayService } from './src/OverlayService/OverlayService'; -export { default as ImageService } from './src/ImageService/ImageService'; export { default as RulesService } from './src/RulesService/RulesService'; export { default as SchemaService } from './src/SchemaService/SchemaService'; - export { default as ShortCutsService } from './src/ShortCutsService/ShortCutsService'; +export { default as AbstractNodeView } from './src/PortalService/AbstractNodeView'; export { default as Tools } from './src/lib/Tools'; -export { default as AbstractNodeView } from './src/PortalService/AbstractNodeView'; export { default as ToolGroup } from './src/lib/ToolGroup'; /* All Elements services */ export { default as BaseService } from './src/BaseService/BaseService'; +export { default as ImageService } from './src/ImageService/ImageService'; export { default as InlineAnnotationsService } from './src/InlineAnnotations/InlineAnnotationsService'; export { default as ListsService } from './src/ListsService/ListsService'; export { default as TablesService } from './src/TablesService/TablesService'; diff --git a/wax-prosemirror-services/src/SchemaService/Mark.js b/wax-prosemirror-services/src/SchemaService/Mark.js index 8d1cd43e7f51869a9d67a5c5b5f1a88d46bf3681..268f68395eadb6ff77413aef71035db98fa45d2a 100644 --- a/wax-prosemirror-services/src/SchemaService/Mark.js +++ b/wax-prosemirror-services/src/SchemaService/Mark.js @@ -1,7 +1,7 @@ /* eslint-disable no-underscore-dangle */ +import { Middleware } from 'wax-prosemirror-core'; import { isPlainObject } from 'lodash'; import ParseRule from './ParseRule'; -import Middleware from '../lib/Middleware'; export default class Mark { name = ''; diff --git a/wax-prosemirror-services/src/SchemaService/Node.js b/wax-prosemirror-services/src/SchemaService/Node.js index 8fa643f92d8d870c99b6acaae8a72f600831df1e..095d2bb02c230137a080f6f3e82180a90a1874ba 100644 --- a/wax-prosemirror-services/src/SchemaService/Node.js +++ b/wax-prosemirror-services/src/SchemaService/Node.js @@ -1,7 +1,7 @@ /* eslint-disable no-underscore-dangle */ +import { Middleware } from 'wax-prosemirror-core'; import { isPlainObject } from 'lodash'; import ParseRule from './ParseRule'; -import Middleware from '../lib/Middleware'; export default class Node { name = ''; diff --git a/wax-prosemirror-services/src/SchemaService/ParseRule.js b/wax-prosemirror-services/src/SchemaService/ParseRule.js index 4b9ec92ee1e230aa3d189de2baf7cb766340fb8c..250f3ea7b53d8aa72e2576f4249c67b3dbb9398e 100644 --- a/wax-prosemirror-services/src/SchemaService/ParseRule.js +++ b/wax-prosemirror-services/src/SchemaService/ParseRule.js @@ -1,5 +1,5 @@ -import { omit } from "lodash"; -import Middleware from "../lib/Middleware"; +import { Middleware } from 'wax-prosemirror-core'; +import { omit } from 'lodash'; export default class ParseRule { tag = null; @@ -44,7 +44,7 @@ export default class ParseRule { exporter.go({ dom }, hook => { hooks = hook; }); - return omit(hooks, ["dom"]); + return omit(hooks, ['dom']); }; }