From ba30023842d1e2553b0e9799483c25622068a24b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E4=BA=91=E9=A3=9E?= Date: Mon, 11 Apr 2022 17:50:29 +0800 Subject: [PATCH] feat:add getValue interface --- src/editor/core/command/Command.ts | 7 +++++++ src/editor/core/command/CommandAdapt.ts | 6 +++++- src/editor/core/draw/Draw.ts | 19 ++++++++++++++++++- src/editor/core/event/CanvasEvent.ts | 24 ++---------------------- 4 files changed, 32 insertions(+), 24 deletions(-) diff --git a/src/editor/core/command/Command.ts b/src/editor/core/command/Command.ts index e9efd6a..8033957 100644 --- a/src/editor/core/command/Command.ts +++ b/src/editor/core/command/Command.ts @@ -2,6 +2,7 @@ import { IElement } from '../..' import { EditorMode } from '../../dataset/enum/Editor' import { RowFlex } from '../../dataset/enum/Row' import { IDrawImagePayload } from '../../interface/Draw' +import { IEditorResult } from '../../interface/Editor' import { IWatermark } from '../../interface/Watermark' import { CommandAdapt } from './CommandAdapt' @@ -55,6 +56,7 @@ export class Command { private static replace: Function private static print: Function private static getImage: Function + private static getValue: Function private static pageScaleRecovery: Function private static pageScaleMinus: Function private static pageScaleAdd: Function @@ -108,6 +110,7 @@ export class Command { Command.replace = adapt.replace.bind(adapt) Command.print = adapt.print.bind(adapt) Command.getImage = adapt.getImage.bind(adapt) + Command.getValue = adapt.getValue.bind(adapt) Command.pageScaleRecovery = adapt.pageScaleRecovery.bind(adapt) Command.pageScaleMinus = adapt.pageScaleMinus.bind(adapt) Command.pageScaleAdd = adapt.pageScaleAdd.bind(adapt) @@ -306,6 +309,10 @@ export class Command { return Command.getImage() } + public getValue(): IEditorResult { + return Command.getValue() + } + // 页面缩放 public executePageScaleRecovery() { return Command.pageScaleRecovery() diff --git a/src/editor/core/command/CommandAdapt.ts b/src/editor/core/command/CommandAdapt.ts index 9a4d8db..c1f9fd9 100644 --- a/src/editor/core/command/CommandAdapt.ts +++ b/src/editor/core/command/CommandAdapt.ts @@ -7,7 +7,7 @@ import { ElementType } from '../../dataset/enum/Element' import { ElementStyleKey } from '../../dataset/enum/ElementStyle' import { RowFlex } from '../../dataset/enum/Row' import { IDrawImagePayload } from '../../interface/Draw' -import { IEditorOption } from '../../interface/Editor' +import { IEditorOption, IEditorResult } from '../../interface/Editor' import { IElement, IElementStyle } from '../../interface/Element' import { IColgroup } from '../../interface/table/Colgroup' import { ITd } from '../../interface/table/Td' @@ -1239,6 +1239,10 @@ export class CommandAdapt { return this.draw.getDataURL() } + public getValue(): IEditorResult { + return this.draw.getValue() + } + public pageScaleRecovery() { const { scale } = this.options if (scale !== 1) { diff --git a/src/editor/core/draw/Draw.ts b/src/editor/core/draw/Draw.ts index 55c8f5a..01be247 100644 --- a/src/editor/core/draw/Draw.ts +++ b/src/editor/core/draw/Draw.ts @@ -1,7 +1,8 @@ +import { version } from '../../../../package.json' import { ZERO } from '../../dataset/constant/Common' import { RowFlex } from '../../dataset/enum/Row' import { IDrawOption, IDrawRowPayload, IDrawRowResult } from '../../interface/Draw' -import { IEditorOption } from '../../interface/Editor' +import { IEditorOption, IEditorResult } from '../../interface/Editor' import { IElement, IElementMetrics, IElementPosition, IElementFillRect, IElementStyle } from '../../interface/Element' import { IRow, IRowElement } from '../../interface/Row' import { deepClone, getUUID } from '../../utils' @@ -35,6 +36,7 @@ import { PageBreakParticle } from './particle/PageBreak' import { Watermark } from './frame/Watermark' import { EditorMode } from '../../dataset/enum/Editor' import { Control } from './control/Control' +import { zipElementList } from '../../utils/element' export class Draw { @@ -362,6 +364,21 @@ export class Draw { } } + public getValue(): IEditorResult { + // 配置 + const { width, height, margins, watermark } = this.options + // 数据 + const data = zipElementList(this.elementList) + return { + version, + width, + height, + margins, + watermark: watermark.data ? watermark : undefined, + data + } + } + private _createPageContainer(): HTMLDivElement { // 容器宽度需跟随纸张宽度 this.container.style.width = `${this.getWidth()}px` diff --git a/src/editor/core/event/CanvasEvent.ts b/src/editor/core/event/CanvasEvent.ts index 112125b..a413a02 100644 --- a/src/editor/core/event/CanvasEvent.ts +++ b/src/editor/core/event/CanvasEvent.ts @@ -1,15 +1,12 @@ -import { version } from '../../../../package.json' import { ElementType } from '../..' import { ZERO } from '../../dataset/constant/Common' import { EDITOR_ELEMENT_COPY_ATTR } from '../../dataset/constant/Element' import { ElementStyleKey } from '../../dataset/enum/ElementStyle' import { MouseEventButton } from '../../dataset/enum/Event' import { KeyMap } from '../../dataset/enum/Keymap' -import { IEditorResult } from '../../interface/Editor' import { IElement } from '../../interface/Element' import { ICurrentPosition } from '../../interface/Position' import { writeTextByElementList } from '../../utils/clipboard' -import { zipElementList } from '../../utils/element' import { Cursor } from '../cursor/Cursor' import { Draw } from '../draw/Draw' import { HyperlinkParticle } from '../draw/particle/HyperlinkParticle' @@ -419,9 +416,8 @@ export class CanvasEvent { this.selectAll() } else if (evt.ctrlKey && evt.key === KeyMap.S) { if (isReadonly) return - const saved = this.listener.saved - if (saved) { - saved(this.save()) + if (this.listener.saved) { + this.listener.saved(this.draw.getValue()) } evt.preventDefault() } @@ -589,20 +585,4 @@ export class CanvasEvent { this.isCompositing = false } - public save(): IEditorResult { - // 配置 - const { width, height, margins, watermark } = this.draw.getOptions() - // 数据 - const elementList = this.draw.getOriginalElementList() - const data = zipElementList(elementList) - return { - version, - width, - height, - margins, - watermark: watermark.data ? watermark : undefined, - data - } - } - } \ No newline at end of file