feat:add continuity page mode

This commit is contained in:
黄云飞
2022-05-27 16:47:23 +08:00
parent 7c37e84497
commit 361e4394f7
14 changed files with 187 additions and 31 deletions
+8 -2
View File
@@ -1,5 +1,5 @@
import { IElement } from '../..'
import { EditorMode } from '../../dataset/enum/Editor'
import { EditorMode, PageMode } from '../../dataset/enum/Editor'
import { RowFlex } from '../../dataset/enum/Row'
import { IDrawImagePayload, IPainterOptions } from '../../interface/Draw'
import { IEditorResult } from '../../interface/Editor'
@@ -57,6 +57,7 @@ export class Command {
private static print: Function
private static getImage: Function
private static getValue: Function
private static pageMode: Function
private static pageScaleRecovery: Function
private static pageScaleMinus: Function
private static pageScaleAdd: Function
@@ -111,6 +112,7 @@ export class Command {
Command.print = adapt.print.bind(adapt)
Command.getImage = adapt.getImage.bind(adapt)
Command.getValue = adapt.getValue.bind(adapt)
Command.pageMode = adapt.pageMode.bind(adapt)
Command.pageScaleRecovery = adapt.pageScaleRecovery.bind(adapt)
Command.pageScaleMinus = adapt.pageScaleMinus.bind(adapt)
Command.pageScaleAdd = adapt.pageScaleAdd.bind(adapt)
@@ -313,7 +315,11 @@ export class Command {
return Command.getValue()
}
// 页面缩放
// 页面模式、页面缩放
public executePageMode(payload: PageMode) {
return Command.pageMode(payload)
}
public executePageScaleRecovery() {
return Command.pageScaleRecovery()
}
+5 -1
View File
@@ -2,7 +2,7 @@ import { WRAP, ZERO } from '../../dataset/constant/Common'
import { EDITOR_ELEMENT_STYLE_ATTR } from '../../dataset/constant/Element'
import { defaultWatermarkOption } from '../../dataset/constant/Watermark'
import { ControlComponent } from '../../dataset/enum/Control'
import { EditorContext, EditorMode } from '../../dataset/enum/Editor'
import { EditorContext, EditorMode, PageMode } from '../../dataset/enum/Editor'
import { ElementType } from '../../dataset/enum/Element'
import { ElementStyleKey } from '../../dataset/enum/ElementStyle'
import { RowFlex } from '../../dataset/enum/Row'
@@ -1243,6 +1243,10 @@ export class CommandAdapt {
return this.draw.getValue()
}
public pageMode(payload: PageMode) {
this.draw.setPageMode(payload)
}
public pageScaleRecovery() {
const { scale } = this.options
if (scale !== 1) {
+62 -14
View File
@@ -34,7 +34,7 @@ import { SubscriptParticle } from './particle/Subscript'
import { SeparatorParticle } from './particle/Separator'
import { PageBreakParticle } from './particle/PageBreak'
import { Watermark } from './frame/Watermark'
import { EditorMode } from '../../dataset/enum/Editor'
import { EditorMode, PageMode } from '../../dataset/enum/Editor'
import { Control } from './control/Control'
import { zipElementList } from '../../utils/element'
import { CheckboxParticle } from './particle/CheckboxParticle'
@@ -97,7 +97,7 @@ export class Draw {
this.pageList = []
this.ctxList = []
this.pageNo = 0
this.mode = options.defaultMode
this.mode = options.mode
this.options = options
this.elementList = elementList
this.listener = listener
@@ -168,6 +168,16 @@ export class Draw {
return Math.floor(this.options.height * this.options.scale)
}
public getCanvasWidth(): number {
const page = this.getPage()
return page.width
}
public getCanvasHeight(): number {
const page = this.getPage()
return page.height
}
public getInnerWidth(): number {
const width = this.getWidth()
const margins = this.getMargins()
@@ -394,6 +404,26 @@ export class Draw {
})
}
public setPageMode(payload: PageMode) {
if (!payload || this.options.pageMode === payload) return
this.options.pageMode = payload
// 纸张大小重置
if (payload === PageMode.PAGING) {
const { height } = this.options
const dpr = window.devicePixelRatio
const canvas = this.pageList[0]
canvas.style.height = `${height}px`
canvas.height = height * dpr
}
this.render({ isSubmitHistory: false, isSetCursor: false })
// 回调
setTimeout(() => {
if (this.listener.pageModeChange) {
this.listener.pageModeChange(payload)
}
})
}
public setPageScale(payload: number) {
this.options.scale = payload
const width = this.getWidth()
@@ -855,12 +885,12 @@ export class Draw {
}
private _drawPage(positionList: IElementPosition[], rowList: IRow[], pageNo: number) {
const width = this.getWidth()
const height = this.getHeight()
const { pageMode } = this.options
const margins = this.getMargins()
const innerWidth = this.getInnerWidth()
const ctx = this.ctxList[pageNo]
ctx.clearRect(0, 0, width, height)
const pageDom = this.pageList[pageNo]
ctx.clearRect(0, 0, pageDom.width, pageDom.height)
// 绘制背景
this.background.render(ctx)
// 绘制页边距
@@ -891,12 +921,13 @@ export class Draw {
this.search.render(ctx, pageNo)
}
// 绘制水印
if (this.options.watermark.data) {
if (pageMode !== PageMode.CONTINUITY && this.options.watermark.data) {
this.waterMark.render(ctx)
}
}
public render(payload?: IDrawOption) {
const { pageMode } = this.options
const {
isSubmitHistory = true,
isSetCursor = true,
@@ -922,15 +953,32 @@ export class Draw {
let pageHeight = marginHeight
let pageNo = 0
const pageRowList: IRow[][] = [[]]
for (let i = 0; i < this.rowList.length; i++) {
const row = this.rowList[i]
if (row.height + pageHeight > height || this.rowList[i - 1]?.isPageBreak) {
pageHeight = marginHeight + row.height
pageRowList.push([row])
pageNo++
if (pageMode === PageMode.CONTINUITY) {
pageRowList[0] = this.rowList
// 重置高度
pageHeight += this.rowList.reduce((pre, cur) => pre + cur.height, 0)
const dpr = window.devicePixelRatio
const pageDom = this.pageList[0]
const pageDomHeight = Number(pageDom.style.height.replace('px', ''))
if (pageHeight > pageDomHeight) {
pageDom.style.height = `${pageHeight}px`
pageDom.height = pageHeight * dpr
} else {
pageHeight += row.height
pageRowList[pageNo].push(row)
const reduceHeight = pageHeight < height ? height : pageHeight
pageDom.style.height = `${reduceHeight}px`
pageDom.height = reduceHeight * dpr
}
} else {
for (let i = 0; i < this.rowList.length; i++) {
const row = this.rowList[i]
if (row.height + pageHeight > height || this.rowList[i - 1]?.isPageBreak) {
pageHeight = marginHeight + row.height
pageRowList.push([row])
pageNo++
} else {
pageHeight += row.height
pageRowList[pageNo].push(row)
}
}
}
// 绘制元素
+5 -2
View File
@@ -1,3 +1,4 @@
import { PageMode } from '../../../dataset/enum/Editor'
import { IEditorOption } from '../../../interface/Editor'
import { Draw } from '../Draw'
@@ -12,9 +13,11 @@ export class Margin {
}
public render(ctx: CanvasRenderingContext2D) {
const { marginIndicatorColor } = this.options
const { marginIndicatorColor, pageMode } = this.options
const width = this.draw.getWidth()
const height = this.draw.getHeight()
const height = pageMode === PageMode.CONTINUITY
? this.draw.getCanvasHeight()
: this.draw.getHeight()
const margins = this.draw.getMargins()
const marginIndicatorSize = this.draw.getMarginIndicatorSize()
ctx.save()
+5 -2
View File
@@ -1,3 +1,4 @@
import { PageMode } from '../../../dataset/enum/Editor'
import { IEditorOption } from '../../../interface/Editor'
import { Draw } from '../Draw'
@@ -12,9 +13,11 @@ export class PageNumber {
}
public render(ctx: CanvasRenderingContext2D, pageNo: number) {
const { pageNumberSize, pageNumberFont, scale } = this.options
const { pageNumberSize, pageNumberFont, scale, pageMode } = this.options
const width = this.draw.getWidth()
const height = this.draw.getHeight()
const height = pageMode === PageMode.CONTINUITY
? this.draw.getCanvasHeight()
: this.draw.getHeight()
const pageNumberBottom = this.draw.getPageNumberBottom()
ctx.save()
ctx.fillStyle = '#00000'
+3
View File
@@ -2,6 +2,7 @@ import {
IContentChange,
IControlChange,
IIntersectionPageNoChange,
IPageModeChange,
IPageScaleChange,
IPageSizeChange,
IRangeStyleChange,
@@ -19,6 +20,7 @@ export class Listener {
public saved: ISaved | null
public contentChange: IContentChange | null
public controlChange: IControlChange | null
public pageModeChange: IPageModeChange | null
constructor() {
this.rangeStyleChange = null
@@ -29,6 +31,7 @@ export class Listener {
this.saved = null
this.contentChange = null
this.controlChange = null
this.pageModeChange = null
}
}
+5
View File
@@ -16,4 +16,9 @@ export enum EditorMode {
EDIT = 'edit',
CLEAN = 'clean',
READONLY = 'readonly'
}
export enum PageMode {
PAGING = 'paging',
CONTINUITY = 'continuity'
}
+5 -3
View File
@@ -13,7 +13,7 @@ import { globalMenus } from './core/contextmenu/menus/globalMenus'
import { ContextMenu } from './core/contextmenu/ContextMenu'
import { tableMenus } from './core/contextmenu/menus/tableMenus'
import { IContextMenuContext, IRegisterContextMenu } from './interface/contextmenu/ContextMenu'
import { EditorComponent, EditorMode } from './dataset/enum/Editor'
import { EditorComponent, EditorMode, PageMode } from './dataset/enum/Editor'
import { EDITOR_COMPONENT } from './dataset/constant/Editor'
import { IHeader } from './interface/Header'
import { IWatermark } from './interface/Watermark'
@@ -51,7 +51,7 @@ export default class Editor {
}
const editorOptions: DeepRequired<IEditorOption> = {
defaultMode: EditorMode.EDIT,
mode: EditorMode.EDIT,
defaultType: 'TEXT',
defaultFont: 'Yahei',
defaultSize: 16,
@@ -77,6 +77,7 @@ export default class Editor {
marginIndicatorSize: 35,
marginIndicatorColor: '#BABABA',
margins: [100, 120, 100, 120],
pageMode: PageMode.PAGING,
tdPadding: 5,
defaultTdHeight: 40,
defaultHyperlinkColor: '#0000FF',
@@ -116,7 +117,8 @@ export {
ElementType,
ControlType,
EditorComponent,
EDITOR_COMPONENT
EDITOR_COMPONENT,
PageMode
}
// 对外类型
+3 -2
View File
@@ -1,12 +1,12 @@
import { IElement } from '..'
import { EditorMode } from '../dataset/enum/Editor'
import { EditorMode, PageMode } from '../dataset/enum/Editor'
import { ICheckboxOption } from './Checkbox'
import { IControlOption } from './Control'
import { IHeader } from './Header'
import { IWatermark } from './Watermark'
export interface IEditorOption {
defaultMode?: EditorMode;
mode?: EditorMode;
defaultType?: string;
defaultFont?: string;
defaultSize?: number;
@@ -32,6 +32,7 @@ export interface IEditorOption {
marginIndicatorSize?: number;
marginIndicatorColor?: string,
margins?: [top: number, right: number, bottom: number, left: number],
pageMode?: PageMode;
tdPadding?: number;
defaultTdHeight?: number;
defaultHyperlinkColor?: string;
+4 -2
View File
@@ -1,4 +1,4 @@
import { ElementType } from '..'
import { ElementType, PageMode } from '..'
import { RowFlex } from '../dataset/enum/Row'
import { IControl } from './Control'
import { IEditorResult } from './Editor'
@@ -34,4 +34,6 @@ export type ISaved = (payload: IEditorResult) => void
export type IContentChange = () => void
export type IControlChange = (payload: IControl | null) => void
export type IControlChange = (payload: IControl | null) => void
export type IPageModeChange = (payload: PageMode) => void