From 0c25cf795fa44e97bc98b75aa37c11e3505789e8 Mon Sep 17 00:00:00 2001 From: parisma Date: Fri, 9 Sep 2022 19:12:36 +0800 Subject: [PATCH] =?UTF-8?q?feat(editor):=20=E4=BB=A3=E7=A0=81=E5=9D=97?= =?UTF-8?q?=E7=BB=91=E5=AE=9A=E5=8A=9F=E8=83=BD=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/editor/src/fields/CodeSelect.vue | 46 +++++++ packages/editor/src/index.ts | 3 +- .../sidebar/code-block/CodeBlockEditor.vue | 53 ++++++-- .../sidebar/code-block/CodeBlockList.vue | 20 +-- packages/editor/src/services/codeBlock.ts | 117 +++++++++++++++++- packages/editor/src/theme/code-block.scss | 15 +++ packages/editor/src/type.ts | 13 +- packages/editor/src/utils/props.ts | 43 +++---- 8 files changed, 259 insertions(+), 51 deletions(-) create mode 100644 packages/editor/src/fields/CodeSelect.vue diff --git a/packages/editor/src/fields/CodeSelect.vue b/packages/editor/src/fields/CodeSelect.vue new file mode 100644 index 00000000..25b8f7b7 --- /dev/null +++ b/packages/editor/src/fields/CodeSelect.vue @@ -0,0 +1,46 @@ + + + diff --git a/packages/editor/src/index.ts b/packages/editor/src/index.ts index 2477b1e5..838c1de3 100644 --- a/packages/editor/src/index.ts +++ b/packages/editor/src/index.ts @@ -19,6 +19,7 @@ import { App } from 'vue'; import Code from './fields/Code.vue'; import CodeLink from './fields/CodeLink.vue'; +import CodeSelect from './fields/CodeSelect.vue'; import uiSelect from './fields/UISelect.vue'; import CodeEditor from './layouts/CodeEditor.vue'; import { setConfig } from './utils/config'; @@ -56,11 +57,11 @@ export default { // eslint-disable-next-line no-param-reassign app.config.globalProperties.$TMAGIC_EDITOR = option; setConfig(option); - app.component(Editor.name, Editor); app.component('m-fields-ui-select', uiSelect); app.component('m-fields-code-link', CodeLink); app.component('m-fields-vs-code', Code); app.component(CodeEditor.name, CodeEditor); + app.component('m-fields-code-select', CodeSelect); }, }; diff --git a/packages/editor/src/layouts/sidebar/code-block/CodeBlockEditor.vue b/packages/editor/src/layouts/sidebar/code-block/CodeBlockEditor.vue index 0f556930..6071ddde 100644 --- a/packages/editor/src/layouts/sidebar/code-block/CodeBlockEditor.vue +++ b/packages/editor/src/layouts/sidebar/code-block/CodeBlockEditor.vue @@ -1,13 +1,31 @@ diff --git a/packages/editor/src/layouts/sidebar/code-block/CodeBlockList.vue b/packages/editor/src/layouts/sidebar/code-block/CodeBlockList.vue index b311550c..8b9571b1 100644 --- a/packages/editor/src/layouts/sidebar/code-block/CodeBlockList.vue +++ b/packages/editor/src/layouts/sidebar/code-block/CodeBlockList.vue @@ -2,7 +2,7 @@
- 新增代码块 + 新增代码块
@@ -23,7 +23,7 @@
- + @@ -32,19 +32,20 @@ diff --git a/packages/editor/src/services/codeBlock.ts b/packages/editor/src/services/codeBlock.ts index 3045f288..8a3235be 100644 --- a/packages/editor/src/services/codeBlock.ts +++ b/packages/editor/src/services/codeBlock.ts @@ -17,8 +17,10 @@ */ import { reactive } from 'vue'; +import { keys, pick } from 'lodash-es'; import type { CodeBlockContent, CodeBlockDSL, CodeState } from '../type'; +import { EditorMode } from '../type'; import { info } from '../utils/logger'; import BaseService from './BaseService'; @@ -27,6 +29,10 @@ class CodeBlock extends BaseService { private state = reactive({ isShowCodeEditor: false, codeDsl: null, + id: '', + editable: true, + mode: EditorMode.EDITOR, + combineIds: [], }); constructor() { @@ -57,14 +63,15 @@ class CodeBlock extends BaseService { * @param {string} id 代码块id * @returns {CodeBlockContent | null} */ - public getCodeDslById(id: string): CodeBlockContent | null { + public getCodeContentById(id: string): CodeBlockContent | null { + if (!id) return null; const totalCodeDsl = this.getCodeDsl(); if (!totalCodeDsl) return null; return totalCodeDsl[id] ?? null; } /** - * 根据代码块id设置代码块内容 + * 设置代码块ID和代码内容到源dsl * @param {string} id 代码块id * @param {CodeBlockContent} codeConfig 代码块内容配置信息 * @returns {void} @@ -78,6 +85,16 @@ class CodeBlock extends BaseService { this.setCodeDsl(codeDsl); } + /** + * 根据代码块id数组获取代码dsl + * @param {string[]} ids 代码块id数组 + * @returns {CodeBlockDSL} + */ + public getCodeDslByIds(ids: string[]): CodeBlockDSL { + const codeDsl = this.getCodeDsl(); + return pick(codeDsl, ids) as CodeBlockDSL; + } + /** * 设置代码编辑面板展示状态 * @param {boolean} status 是否展示代码编辑面板 @@ -95,12 +112,106 @@ class CodeBlock extends BaseService { return this.state.isShowCodeEditor; } + /** + * 设置代码编辑面板展示状态及展示内容 + * @param {boolean} status 是否展示代码编辑面板 + * @param {string} id 代码块id + * @returns {void} + */ + public setCodeEditorContent(status: boolean, id: string): void { + if (!id) return; + this.setId(id); + this.state.isShowCodeEditor = status; + } + + /** + * 获取当前选中的代码块内容 + * @returns {CodeBlockContent | null} + */ + public getCurrentDsl() { + return this.getCodeContentById(this.state.id); + } + + /** + * 获取编辑状态 + * @returns {boolean} 是否可编辑 + */ + public getEditStatus(): boolean { + return this.state.editable; + } + + /** + * 设置编辑状态 + * @param {boolean} 是否可编辑 + * @returns {void} + */ + public setEditStatus(status: boolean): void { + this.state.editable = status; + } + + /** + * 设置当前选中的代码块ID + * @param {string} id 代码块id + * @returns {void} + */ + public setId(id: string) { + if (!id) return; + this.state.id = id; + } + + /** + * 获取当前选中的代码块ID + * @returns {string} id 代码块id + */ + public getId(): string { + return this.state.id; + } + + /** + * 获取当前模式 + * @returns {EditorMode} + */ + public getMode(): EditorMode { + return this.state.mode; + } + + /** + * 设置当前模式 + * @param {EditorMode} mode 模式 + * @returns {void} + */ + public setMode(mode: EditorMode): void { + this.state.mode = mode; + } + + /** + * 设置当前已关联绑定的代码块id数组 + * @param {string[]} ids 代码块id数组 + * @returns {void} + */ + public setCombineIds(ids: string[]): void { + this.state.combineIds = ids; + } + + /** + * 获取当前已关联绑定的代码块id数组 + * @returns {string[]} + */ + public getCombineIds(): string[] { + return this.state.combineIds; + } + /** * 生成代码块唯一id * @returns {string} 代码块唯一id */ public getUniqueId(): string { - return (Date.now().toString(36) + Math.random().toString(36).substring(2)).padEnd(19, '0'); + const newId = (Date.now().toString(36) + Math.random().toString(36).substring(2)).padEnd(19, '0'); + // 判断是否重复 + const dsl = this.getCodeDsl(); + const existedIds = keys(dsl); + if (!existedIds.includes(newId)) return newId; + return this.getUniqueId(); } public destroy() { diff --git a/packages/editor/src/theme/code-block.scss b/packages/editor/src/theme/code-block.scss index df13c5c5..39870a66 100644 --- a/packages/editor/src/theme/code-block.scss +++ b/packages/editor/src/theme/code-block.scss @@ -70,3 +70,18 @@ } } } +.m-fields-code-select { + display: flex; + width: 100%; + align-items: center; +} +.code-editor-side-menu { + width: 20%; +} +.m-editor-code-block-editor-panel-list-mode { + width: 80%; + position: absolute; + height: 90%; + left: 20%; + top: 60px; +} diff --git a/packages/editor/src/type.ts b/packages/editor/src/type.ts index 7784b8c8..12fe67ab 100644 --- a/packages/editor/src/type.ts +++ b/packages/editor/src/type.ts @@ -322,6 +322,17 @@ export interface CodeBlockContent { export type CodeState = { /** 是否展示代码块编辑区 */ isShowCodeEditor: boolean; - /** 代码块DSL */ + /** 代码块DSL数据源 */ codeDsl: CodeBlockDSL | null; + /** 当前选中的代码块id */ + id: string; + /** 代码块是否可编辑 */ + editable: boolean; + mode: EditorMode; + combineIds: string[]; }; + +export enum EditorMode { + LIST = 'list', + EDITOR = 'editor', +} diff --git a/packages/editor/src/utils/props.ts b/packages/editor/src/utils/props.ts index 1036272a..65978153 100644 --- a/packages/editor/src/utils/props.ts +++ b/packages/editor/src/utils/props.ts @@ -227,33 +227,24 @@ export const fillConfig = (config: FormConfig = []) => [ lazy: true, items: [ { - type: 'tab', - active: '0', - items: [ - { - title: 'created', - lazy: true, - items: [ - { - labelWidth: '100px', - name: 'created', - text: '关联代码块', - type: 'select', - multiple: true, - options: () => { - const codeDsl = codeBlockService.getCodeDsl(); - if (codeDsl) { - return map(codeDsl, (value, key) => ({ - text: value.name, - value: key, - })); - } - return []; - }, - }, - ], + name: 'created', + text: 'created', + type: 'code-select', + labelWidth: '100px', + selectConfig: { + multiple: true, + options: () => { + const codeDsl = codeBlockService.getCodeDsl(); + if (codeDsl) { + return map(codeDsl, (value, key) => ({ + text: `${value.name}(${key})`, + label: `${value.name}(${key})`, + value: key, + })); + } + return []; }, - ], + }, }, ], },