feat: add hyperlink
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { IElement } from "../.."
|
||||
import { RowFlex } from "../../dataset/enum/Row"
|
||||
import { IDrawImagePayload } from "../../interface/Draw"
|
||||
import { CommandAdapt } from "./CommandAdapt"
|
||||
@@ -34,6 +35,7 @@ export class Command {
|
||||
private static deleteTableCol: Function
|
||||
private static deleteTable: Function
|
||||
private static image: Function
|
||||
private static hyperlink: Function
|
||||
private static search: Function
|
||||
private static print: Function
|
||||
private static pageScaleRecovery: Function
|
||||
@@ -71,6 +73,7 @@ export class Command {
|
||||
Command.deleteTableCol = adapt.deleteTableCol.bind(adapt)
|
||||
Command.deleteTable = adapt.deleteTable.bind(adapt)
|
||||
Command.image = adapt.image.bind(adapt)
|
||||
Command.hyperlink = adapt.hyperlink.bind(adapt)
|
||||
Command.search = adapt.search.bind(adapt)
|
||||
Command.print = adapt.print.bind(adapt)
|
||||
Command.pageScaleRecovery = adapt.pageScaleRecovery.bind(adapt)
|
||||
@@ -165,7 +168,7 @@ export class Command {
|
||||
return Command.rowMargin(payload)
|
||||
}
|
||||
|
||||
// 表格、图片上传、搜索、打印
|
||||
// 表格、图片上传、超链接、搜索、打印
|
||||
public executeInsertTable(row: number, col: number) {
|
||||
return Command.insertTable(row, col)
|
||||
}
|
||||
@@ -198,6 +201,10 @@ export class Command {
|
||||
return Command.deleteTable()
|
||||
}
|
||||
|
||||
public executeHyperlink(payload: IElement) {
|
||||
return Command.hyperlink(payload)
|
||||
}
|
||||
|
||||
public executeImage(payload: IDrawImagePayload) {
|
||||
return Command.image(payload)
|
||||
}
|
||||
|
||||
@@ -654,6 +654,30 @@ export class CommandAdapt {
|
||||
}
|
||||
}
|
||||
|
||||
public hyperlink(payload: IElement) {
|
||||
const { startIndex, endIndex } = this.range.getRange()
|
||||
if (startIndex === 0 && endIndex === 0) return
|
||||
const elementList = this.draw.getElementList()
|
||||
const { valueList, url } = payload
|
||||
const hyperlinkId = getUUID()
|
||||
const newElementList = valueList?.map<IElement>(v => ({
|
||||
url,
|
||||
hyperlinkId,
|
||||
value: v.value,
|
||||
type: ElementType.HYPERLINK
|
||||
}))
|
||||
if (!newElementList) return
|
||||
const start = startIndex + 1
|
||||
if (startIndex === endIndex) {
|
||||
elementList.splice(start, 0, ...newElementList)
|
||||
} else {
|
||||
elementList.splice(start, endIndex - startIndex, ...newElementList)
|
||||
}
|
||||
const curIndex = start + newElementList.length - 1
|
||||
this.range.setRange(curIndex, curIndex)
|
||||
this.draw.render({ curIndex })
|
||||
}
|
||||
|
||||
public image(payload: IDrawImagePayload) {
|
||||
const { startIndex, endIndex } = this.range.getRange()
|
||||
if (startIndex === 0 && endIndex === 0) return
|
||||
@@ -706,7 +730,8 @@ export class CommandAdapt {
|
||||
// 搜索文本
|
||||
function searchClosure(payload: string | null, type: EditorContext, elementList: IElement[], restArgs?: ISearchResultRestArgs) {
|
||||
if (!payload) return
|
||||
const text = elementList.map(e => !e.type || e.type === ElementType.TEXT ? e.value : ZERO)
|
||||
const { TEXT, HYPERLINK } = ElementType
|
||||
const text = elementList.map(e => !e.type || e.type === TEXT || e.type === HYPERLINK ? e.value : ZERO)
|
||||
.filter(Boolean)
|
||||
.join('')
|
||||
const matchStartIndexList = []
|
||||
|
||||
@@ -26,6 +26,7 @@ import { GlobalObserver } from "../observer/GlobalObserver"
|
||||
import { TableParticle } from "./particle/table/TableParticle"
|
||||
import { ISearchResult } from "../../interface/Search"
|
||||
import { TableTool } from "./particle/table/TableTool"
|
||||
import { HyperlinkParticle } from "./particle/HyperlinkParticle"
|
||||
|
||||
export class Draw {
|
||||
|
||||
@@ -54,6 +55,7 @@ export class Draw {
|
||||
private tableParticle: TableParticle
|
||||
private tableTool: TableTool
|
||||
private pageNumber: PageNumber
|
||||
private hyperlinkParticle: HyperlinkParticle
|
||||
|
||||
private rowList: IRow[]
|
||||
private painterStyle: IElementStyle | null
|
||||
@@ -92,6 +94,7 @@ export class Draw {
|
||||
this.tableParticle = new TableParticle(this)
|
||||
this.tableTool = new TableTool(this)
|
||||
this.pageNumber = new PageNumber(this)
|
||||
this.hyperlinkParticle = new HyperlinkParticle(this)
|
||||
new GlobalObserver(this)
|
||||
|
||||
this.canvasEvent = new CanvasEvent(this)
|
||||
@@ -251,6 +254,10 @@ export class Draw {
|
||||
return this.tableTool
|
||||
}
|
||||
|
||||
public getHyperlinkParticle(): HyperlinkParticle {
|
||||
return this.hyperlinkParticle
|
||||
}
|
||||
|
||||
public getRowCount(): number {
|
||||
return this.rowList.length
|
||||
}
|
||||
@@ -431,11 +438,10 @@ export class Draw {
|
||||
const ascent = metrics.boundingBoxAscent + rowMargin
|
||||
const descent = metrics.boundingBoxDescent + rowMargin
|
||||
const height = ascent + descent
|
||||
const rowElement: IRowElement = {
|
||||
...element,
|
||||
const rowElement: IRowElement = Object.assign(element, {
|
||||
metrics,
|
||||
style: this._getFont(element, scale)
|
||||
}
|
||||
})
|
||||
// 超过限定宽度
|
||||
const preElement = elementList[i - 1]
|
||||
if (
|
||||
@@ -509,9 +515,21 @@ export class Draw {
|
||||
}
|
||||
}
|
||||
positionList.push(positionItem)
|
||||
// 元素绘制
|
||||
if (element.type === ElementType.IMAGE) {
|
||||
this.textParticle.complete()
|
||||
this.imageParticle.render(ctx, element, x, y + offsetY)
|
||||
} else if (element.type === ElementType.TABLE) {
|
||||
this.tableParticle.render(ctx, element, x, y)
|
||||
} else if (element.type === ElementType.HYPERLINK) {
|
||||
this.textParticle.complete()
|
||||
this.hyperlinkParticle.render(ctx, element, x, y + offsetY)
|
||||
} else {
|
||||
this.textParticle.record(ctx, element, x, y + offsetY)
|
||||
}
|
||||
// 下划线绘制
|
||||
if (element.underline) {
|
||||
this.underline.render(ctx, x, y + curRow.height, metrics.width)
|
||||
this.underline.render(ctx, element.color!, x, y + curRow.height, metrics.width)
|
||||
}
|
||||
// 删除线绘制
|
||||
if (element.strikeout) {
|
||||
@@ -521,15 +539,6 @@ export class Draw {
|
||||
if (element.highlight) {
|
||||
this.highlight.render(ctx, element.highlight, x, y, metrics.width, curRow.height)
|
||||
}
|
||||
// 元素绘制
|
||||
if (element.type === ElementType.IMAGE) {
|
||||
this.textParticle.complete()
|
||||
this.imageParticle.render(ctx, element, x, y + offsetY)
|
||||
} else if (element.type === ElementType.TABLE) {
|
||||
this.tableParticle.render(ctx, element, x, y)
|
||||
} else {
|
||||
this.textParticle.record(ctx, element, x, y + offsetY)
|
||||
}
|
||||
// 选区绘制
|
||||
const { startIndex, endIndex } = this.range.getRange()
|
||||
if (startIndex !== endIndex && startIndex < index && index <= endIndex) {
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
import { IElement } from "../../.."
|
||||
import { IEditorOption } from "../../../interface/Editor"
|
||||
import { IElementPosition } from "../../../interface/Element"
|
||||
import { IRowElement } from "../../../interface/Row"
|
||||
import { Draw } from "../Draw"
|
||||
|
||||
export class HyperlinkParticle {
|
||||
|
||||
private draw: Draw
|
||||
private options: Required<IEditorOption>
|
||||
private container: HTMLDivElement
|
||||
private hyperlinkPopupContainer: HTMLDivElement
|
||||
private hyperlinkDom: HTMLAnchorElement
|
||||
|
||||
constructor(draw: Draw) {
|
||||
this.draw = draw
|
||||
this.options = draw.getOptions()
|
||||
this.container = draw.getContainer()
|
||||
const { hyperlinkPopupContainer, hyperlinkDom } = this._createHyperlinkPopupDom()
|
||||
this.hyperlinkDom = hyperlinkDom
|
||||
this.hyperlinkPopupContainer = hyperlinkPopupContainer
|
||||
}
|
||||
|
||||
private _createHyperlinkPopupDom() {
|
||||
const hyperlinkPopupContainer = document.createElement('div')
|
||||
hyperlinkPopupContainer.classList.add('hyperlink-popup')
|
||||
const hyperlinkDom = document.createElement('a')
|
||||
hyperlinkDom.target = '_blank'
|
||||
hyperlinkPopupContainer.append(hyperlinkDom)
|
||||
this.container.append(hyperlinkPopupContainer)
|
||||
return { hyperlinkPopupContainer, hyperlinkDom }
|
||||
}
|
||||
|
||||
public drawHyperlinkPopup(element: IElement, position: IElementPosition) {
|
||||
const { coordinate: { leftTop: [left, top] }, lineHeight } = position
|
||||
const height = this.draw.getHeight()
|
||||
const pageGap = this.draw.getPageGap()
|
||||
const preY = this.draw.getPageNo() * (height + pageGap)
|
||||
// 位置
|
||||
this.hyperlinkPopupContainer.style.display = 'block'
|
||||
this.hyperlinkPopupContainer.style.left = `${left}px`
|
||||
this.hyperlinkPopupContainer.style.top = `${top + preY + lineHeight}px`
|
||||
// 标签
|
||||
const url = element.url || '#'
|
||||
this.hyperlinkDom.href = url
|
||||
this.hyperlinkDom.innerText = url
|
||||
}
|
||||
|
||||
public clearHyperlinkPopup() {
|
||||
this.hyperlinkPopupContainer.style.display = 'none'
|
||||
}
|
||||
|
||||
public render(ctx: CanvasRenderingContext2D, element: IRowElement, x: number, y: number) {
|
||||
ctx.save()
|
||||
ctx.font = element.style
|
||||
if (!element.color) {
|
||||
element.color = this.options.defaultHyperlinkColor
|
||||
}
|
||||
ctx.fillStyle = element.color
|
||||
if (element.underline === undefined) {
|
||||
element.underline = true
|
||||
}
|
||||
ctx.fillText(element.value, x, y)
|
||||
ctx.restore()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,10 +9,10 @@ export class Underline {
|
||||
this.options = draw.getOptions()
|
||||
}
|
||||
|
||||
public render(ctx: CanvasRenderingContext2D, x: number, y: number, width: number) {
|
||||
public render(ctx: CanvasRenderingContext2D, color: string, x: number, y: number, width: number) {
|
||||
const { underlineColor } = this.options
|
||||
ctx.save()
|
||||
ctx.strokeStyle = underlineColor
|
||||
ctx.strokeStyle = color || underlineColor
|
||||
ctx.beginPath()
|
||||
ctx.moveTo(x, y)
|
||||
ctx.lineTo(x + width, y)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { ElementType } from "../.."
|
||||
import { ZERO } from "../../dataset/constant/Common"
|
||||
import { ElementStyleKey } from "../../dataset/enum/ElementStyle"
|
||||
import { MouseEventButton } from "../../dataset/enum/Event"
|
||||
@@ -6,6 +7,7 @@ import { IElement } from "../../interface/Element"
|
||||
import { writeTextByElementList } from "../../utils/clipboard"
|
||||
import { Cursor } from "../cursor/Cursor"
|
||||
import { Draw } from "../draw/Draw"
|
||||
import { HyperlinkParticle } from "../draw/particle/HyperlinkParticle"
|
||||
import { ImageParticle } from "../draw/particle/ImageParticle"
|
||||
import { TableTool } from "../draw/particle/table/TableTool"
|
||||
import { HistoryManager } from "../history/HistoryManager"
|
||||
@@ -27,6 +29,7 @@ export class CanvasEvent {
|
||||
private historyManager: HistoryManager
|
||||
private imageParticle: ImageParticle
|
||||
private tableTool: TableTool
|
||||
private hyperlinkParticle: HyperlinkParticle
|
||||
|
||||
constructor(draw: Draw) {
|
||||
this.isAllowDrag = false
|
||||
@@ -42,6 +45,7 @@ export class CanvasEvent {
|
||||
this.historyManager = this.draw.getHistoryManager()
|
||||
this.imageParticle = this.draw.getImageParticle()
|
||||
this.tableTool = this.draw.getTableTool()
|
||||
this.hyperlinkParticle = this.draw.getHyperlinkParticle()
|
||||
}
|
||||
|
||||
public register() {
|
||||
@@ -160,20 +164,26 @@ export class CanvasEvent {
|
||||
isComputeRowList: false
|
||||
})
|
||||
}
|
||||
const elementList = this.draw.getElementList()
|
||||
const positionList = this.position.getPositionList()
|
||||
const curIndex = isTable ? tdValueIndex! : index
|
||||
const curElement = elementList[curIndex]
|
||||
// 图片尺寸拖拽组件
|
||||
this.imageParticle.clearResizer()
|
||||
if (isDirectHitImage) {
|
||||
const elementList = this.draw.getElementList()
|
||||
const positionList = this.position.getPositionList()
|
||||
const curIndex = isTable ? tdValueIndex! : index
|
||||
this.imageParticle.drawResizer(elementList[curIndex], positionList[curIndex])
|
||||
this.imageParticle.drawResizer(curElement, positionList[curIndex])
|
||||
}
|
||||
// 表格工具组件
|
||||
this.tableTool.dispose()
|
||||
if (isTable) {
|
||||
const elementList = this.draw.getOriginalElementList()
|
||||
const positionList = this.position.getOriginalPositionList()
|
||||
this.tableTool.render(elementList[index], positionList[index])
|
||||
const originalElementList = this.draw.getOriginalElementList()
|
||||
const originalPositionList = this.position.getOriginalPositionList()
|
||||
this.tableTool.render(originalElementList[index], originalPositionList[index])
|
||||
}
|
||||
// 超链接
|
||||
this.hyperlinkParticle.clearHyperlinkPopup()
|
||||
if (curElement.type === ElementType.HYPERLINK) {
|
||||
this.hyperlinkParticle.drawHyperlinkPopup(curElement, positionList[curIndex])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -315,6 +325,7 @@ export class CanvasEvent {
|
||||
if (!this.cursor) return
|
||||
const cursorPosition = this.position.getCursorPosition()
|
||||
if (!data || !cursorPosition || this.isCompositing) return
|
||||
const { TEXT, HYPERLINK } = ElementType
|
||||
const text = data.replaceAll(`\n`, ZERO)
|
||||
const elementList = this.draw.getElementList()
|
||||
const agentDom = this.cursor.getAgentDom()
|
||||
@@ -330,18 +341,32 @@ export class CanvasEvent {
|
||||
restArg = { tdId, trId, tableId }
|
||||
}
|
||||
const element = elementList[endIndex]
|
||||
const inputData: IElement[] = text.split('').map(value => ({
|
||||
value,
|
||||
font: element.font,
|
||||
size: element.size,
|
||||
bold: element.bold,
|
||||
color: element.color,
|
||||
highlight: element.highlight,
|
||||
italic: element.italic,
|
||||
underline: element.underline,
|
||||
strikeout: element.strikeout,
|
||||
...restArg
|
||||
}))
|
||||
const inputData: IElement[] = text.split('').map(value => {
|
||||
const newElement: IElement = {
|
||||
value,
|
||||
...restArg
|
||||
}
|
||||
if (
|
||||
!element.type
|
||||
|| element.type === TEXT
|
||||
|| (element.type === HYPERLINK && elementList[endIndex + 1]?.type === HYPERLINK)
|
||||
) {
|
||||
if (element.type) {
|
||||
newElement.type = element.type
|
||||
}
|
||||
newElement.font = element.font
|
||||
newElement.size = element.size
|
||||
newElement.bold = element.bold
|
||||
newElement.color = element.color
|
||||
newElement.highlight = element.highlight
|
||||
newElement.italic = element.italic
|
||||
newElement.underline = element.underline
|
||||
newElement.strikeout = element.strikeout
|
||||
newElement.url = element.url
|
||||
newElement.hyperlinkId = element.hyperlinkId
|
||||
}
|
||||
return newElement
|
||||
})
|
||||
let start = 0
|
||||
if (isCollspace) {
|
||||
start = index + 1
|
||||
|
||||
@@ -3,6 +3,7 @@ import { IEditorOption } from "../../interface/Editor"
|
||||
import { findParent } from "../../utils"
|
||||
import { Cursor } from "../cursor/Cursor"
|
||||
import { Draw } from "../draw/Draw"
|
||||
import { HyperlinkParticle } from "../draw/particle/HyperlinkParticle"
|
||||
import { ImageParticle } from "../draw/particle/ImageParticle"
|
||||
import { TableTool } from "../draw/particle/table/TableTool"
|
||||
import { RangeManager } from "../range/RangeManager"
|
||||
@@ -18,6 +19,7 @@ export class GlobalEvent {
|
||||
private range: RangeManager
|
||||
private imageParticle: ImageParticle
|
||||
private tableTool: TableTool
|
||||
private hyperlinkParticle: HyperlinkParticle
|
||||
|
||||
constructor(draw: Draw, canvasEvent: CanvasEvent) {
|
||||
this.draw = draw
|
||||
@@ -28,6 +30,7 @@ export class GlobalEvent {
|
||||
this.range = draw.getRange()
|
||||
this.imageParticle = draw.getImageParticle()
|
||||
this.tableTool = draw.getTableTool()
|
||||
this.hyperlinkParticle = draw.getHyperlinkParticle()
|
||||
}
|
||||
|
||||
public register() {
|
||||
@@ -70,6 +73,7 @@ export class GlobalEvent {
|
||||
this.range.setRange(0, 0)
|
||||
this.imageParticle.clearResizer()
|
||||
this.tableTool.dispose()
|
||||
this.hyperlinkParticle.clearHyperlinkPopup()
|
||||
}
|
||||
|
||||
public setDragState() {
|
||||
|
||||
Reference in New Issue
Block a user