From 09c4d53bca9744b115b5e7c94a42ebe81da487b8 Mon Sep 17 00:00:00 2001 From: Hufe921 Date: Thu, 13 Jul 2023 21:05:59 +0800 Subject: [PATCH] feat: get range context info --- docs/guide/command-get.md | 8 +++++++ src/editor/core/command/Command.ts | 2 ++ src/editor/core/command/CommandAdapt.ts | 28 +++++++++++++++++++++++-- src/editor/interface/Range.ts | 9 ++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/docs/guide/command-get.md b/docs/guide/command-get.md index e74b077..cca3cf2 100644 --- a/docs/guide/command-get.md +++ b/docs/guide/command-get.md @@ -48,6 +48,14 @@ const wordCount = await instance.command.getWordCount() const rangeText = instance.command.getRangeText() ``` +## getRangeContext +功能:获取选区上下文 + +用法: +```javascript +const rangeContext = instance.command.getRangeContext() +``` + ## getPaperMargin 功能:获取页边距 diff --git a/src/editor/core/command/Command.ts b/src/editor/core/command/Command.ts index 6b79afd..649deff 100644 --- a/src/editor/core/command/Command.ts +++ b/src/editor/core/command/Command.ts @@ -80,6 +80,7 @@ export class Command { public getValue: CommandAdapt['getValue'] public getWordCount: CommandAdapt['getWordCount'] public getRangeText: CommandAdapt['getRangeText'] + public getRangeContext: CommandAdapt['getRangeContext'] public getPaperMargin: CommandAdapt['getPaperMargin'] public getSearchNavigateInfo: CommandAdapt['getSearchNavigateInfo'] @@ -168,6 +169,7 @@ export class Command { this.getValue = adapt.getValue.bind(adapt) this.getWordCount = adapt.getWordCount.bind(adapt) this.getRangeText = adapt.getRangeText.bind(adapt) + this.getRangeContext = adapt.getRangeContext.bind(adapt) this.getCatalog = adapt.getCatalog.bind(adapt) this.getPaperMargin = adapt.getPaperMargin.bind(adapt) this.getSearchNavigateInfo = adapt.getSearchNavigateInfo.bind(adapt) diff --git a/src/editor/core/command/CommandAdapt.ts b/src/editor/core/command/CommandAdapt.ts index 4c63fc7..92c1fa6 100644 --- a/src/editor/core/command/CommandAdapt.ts +++ b/src/editor/core/command/CommandAdapt.ts @@ -16,12 +16,13 @@ import { IAppendElementListOption, IDrawImagePayload, IGetValueOption, IPainterO import { IEditorData, IEditorOption, IEditorResult } from '../../interface/Editor' import { IElement, IElementStyle } from '../../interface/Element' import { IMargin } from '../../interface/Margin' +import { RangeContext } from '../../interface/Range' import { IColgroup } from '../../interface/table/Colgroup' import { ITd } from '../../interface/table/Td' import { ITr } from '../../interface/table/Tr' import { IWatermark } from '../../interface/Watermark' -import { downloadFile, getUUID } from '../../utils' -import { formatElementContext, formatElementList, isTextLikeElement } from '../../utils/element' +import { deepClone, downloadFile, getUUID } from '../../utils' +import { formatElementContext, formatElementList, isTextLikeElement, pickElementAttr } from '../../utils/element' import { printImageBase64 } from '../../utils/print' import { Control } from '../draw/control/Control' import { Draw } from '../draw/Draw' @@ -1601,6 +1602,29 @@ export class CommandAdapt { return this.range.toString() } + public getRangeContext(): RangeContext | null { + const range = this.range.getRange() + const { startIndex, endIndex } = range + if (!~startIndex && !~endIndex) return null + // 选区信息 + const isCollapsed = startIndex === endIndex + // 元素信息 + const elementList = this.draw.getElementList() + const startElement = pickElementAttr(elementList[isCollapsed ? startIndex : startIndex + 1]) + const endElement = pickElementAttr(elementList[endIndex]) + // 页码信息 + const positionList = this.position.getPositionList() + const startPageNo = positionList[startIndex].pageNo + const endPageNo = positionList[endIndex].pageNo + return deepClone({ + isCollapsed, + startElement, + endElement, + startPageNo, + endPageNo + }) + } + public pageMode(payload: PageMode) { this.draw.setPageMode(payload) } diff --git a/src/editor/interface/Range.ts b/src/editor/interface/Range.ts index 3426996..05ae3f9 100644 --- a/src/editor/interface/Range.ts +++ b/src/editor/interface/Range.ts @@ -1,4 +1,5 @@ import { EditorZone } from '../dataset/enum/Editor' +import { IElement } from './Element' export interface IRange { startIndex: number; @@ -15,3 +16,11 @@ export interface IRange { export type RangeRowArray = Map export type RangeRowMap = Map> + +export type RangeContext = { + isCollapsed: boolean; + startElement: IElement; + endElement: IElement; + startPageNo: number; + endPageNo: number; +} \ No newline at end of file