feat:content block core

This commit is contained in:
Hufe921
2022-11-27 20:11:29 +08:00
parent 4118fd4be3
commit 8f7eb3e67f
15 changed files with 189 additions and 5 deletions
+12
View File
@@ -46,6 +46,7 @@ import { WorkerManager } from '../worker/WorkerManager'
import { Previewer } from './particle/previewer/Previewer'
import { DateParticle } from './particle/date/DateParticle'
import { IMargin } from '../../interface/Margin'
import { BlockParticle } from './particle/block/BlockParticle'
export class Draw {
@@ -86,6 +87,7 @@ export class Draw {
private superscriptParticle: SuperscriptParticle
private subscriptParticle: SubscriptParticle
private checkboxParticle: CheckboxParticle
private blockParticle: BlockParticle
private control: Control
private workerManager: WorkerManager
@@ -138,6 +140,7 @@ export class Draw {
this.superscriptParticle = new SuperscriptParticle()
this.subscriptParticle = new SubscriptParticle()
this.checkboxParticle = new CheckboxParticle(this)
this.blockParticle = new BlockParticle(this)
this.control = new Control(this)
new ScrollObserver(this)
@@ -707,6 +710,11 @@ export class Draw {
metrics.height = defaultSize * scale
metrics.boundingBoxDescent = 0
metrics.boundingBoxAscent = metrics.height
} else if (element.type === ElementType.BLOCK) {
metrics.width = element.width! * scale
metrics.height = element.height! * scale
metrics.boundingBoxDescent = metrics.height
metrics.boundingBoxAscent = 0
} else {
// 设置上下标真实字体尺寸
const size = element.size || this.options.defaultSize
@@ -739,6 +747,7 @@ export class Draw {
const preElement = elementList[i - 1]
if (
preElement?.type === ElementType.TABLE
|| preElement?.type === ElementType.BLOCK
|| preElement?.imgDisplay === ImageDisplay.INLINE
|| element.imgDisplay === ImageDisplay.INLINE
|| curRow.width + metrics.width > innerWidth
@@ -880,6 +889,9 @@ export class Draw {
// 如果是两端对齐,因canvas目前不支持letterSpacing需单独绘制文本
this.textParticle.record(ctx, element, x, y + offsetY)
this._drawRichText(ctx)
} else if (element.type === ElementType.BLOCK) {
this._drawRichText(ctx)
this.blockParticle.render(pageNo, element, x, y)
} else {
this.textParticle.record(ctx, element, x, y + offsetY)
}
@@ -0,0 +1,47 @@
import { IRowElement } from '../../../../interface/Row'
import { Draw } from '../../Draw'
import { BaseBlock } from './modules/BaseBlock'
export class BlockParticle {
private draw: Draw
private container: HTMLDivElement
private blockContainer: HTMLDivElement
private blockMap: Map<string, BaseBlock>
constructor(draw: Draw) {
this.draw = draw
this.container = draw.getContainer()
this.blockMap = new Map()
this.blockContainer = this._createBlockContainer()
this.container.append(this.blockContainer)
}
private _createBlockContainer(): HTMLDivElement {
const blockContainer = document.createElement('div')
blockContainer.classList.add('block-container')
return blockContainer
}
public getDraw(): Draw {
return this.draw
}
public getBlockContainer(): HTMLDivElement {
return this.blockContainer
}
public render(pageNo: number, element: IRowElement, x: number, y: number) {
const id = element.id!
const cacheBlock = this.blockMap.get(id)
if (cacheBlock) {
cacheBlock.setClientRects(pageNo, x, y)
} else {
const newBlock = new BaseBlock(this, element)
newBlock.render()
newBlock.setClientRects(pageNo, x, y)
this.blockMap.set(id, newBlock)
}
}
}
@@ -0,0 +1,51 @@
import { BlockType } from '../../../../../dataset/enum/Block'
import { IRowElement } from '../../../../../interface/Row'
import { Draw } from '../../../Draw'
import { BlockParticle } from '../BlockParticle'
import { IFrameBlock } from './IFrameBlock'
export class BaseBlock {
private draw: Draw
private element: IRowElement
private block: IFrameBlock | null
private blockContainer: HTMLDivElement
private blockItem: HTMLDivElement
constructor(blockParticle: BlockParticle, element: IRowElement) {
this.draw = blockParticle.getDraw()
this.blockContainer = blockParticle.getBlockContainer()
this.element = element
this.block = null
this.blockItem = this._createBlockItem()
this.blockContainer.append(this.blockItem)
}
private _createBlockItem(): HTMLDivElement {
const blockItem = document.createElement('div')
blockItem.classList.add('block-item')
return blockItem
}
public render() {
const block = this.element.block!
if (block.type === BlockType.IFRAME) {
this.block = new IFrameBlock(this.element)
this.block.render(this.blockItem)
}
}
public setClientRects(pageNo: number, x: number, y: number) {
const scale = this.draw.getOptions().scale
const height = this.draw.getHeight()
const pageGap = this.draw.getPageGap()
const preY = pageNo * (height + pageGap)
// 尺寸
this.blockItem.style.width = `${this.element.width! * scale}px`
this.blockItem.style.height = `${this.element.height! * scale}px`
// 位置
this.blockItem.style.left = `${x}px`
this.blockItem.style.top = `${preY + y}px`
}
}
@@ -0,0 +1,28 @@
import { IRowElement } from '../../../../../interface/Row'
export class IFrameBlock {
private static readonly sandbox = [
'allow-forms',
'allow-scripts',
'allow-same-origin',
'allow-popups'
]
private element: IRowElement
constructor(element: IRowElement) {
this.element = element
}
public render(blockItemContainer: HTMLDivElement) {
const block = this.element.block!
const iframe = document.createElement('iframe')
iframe.sandbox.add(...IFrameBlock.sandbox)
iframe.style.border = 'none'
iframe.style.width = '100%'
iframe.style.height = '100%'
iframe.src = block.iframeBlock?.src || ''
blockItemContainer.append(iframe)
}
}