feat: add getText api #240
This commit is contained in:
@@ -114,3 +114,17 @@ const {
|
|||||||
footer: string
|
footer: string
|
||||||
} = await instance.command.getHTML()
|
} = await instance.command.getHTML()
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## getText
|
||||||
|
|
||||||
|
Feature: Get text
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
const {
|
||||||
|
header: string
|
||||||
|
main: string
|
||||||
|
footer: string
|
||||||
|
} = await instance.command.getText()
|
||||||
|
```
|
||||||
|
|||||||
@@ -114,3 +114,17 @@ const {
|
|||||||
footer: string
|
footer: string
|
||||||
} = await instance.command.getHTML()
|
} = await instance.command.getHTML()
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## getText
|
||||||
|
|
||||||
|
功能:获取文本
|
||||||
|
|
||||||
|
用法:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
const {
|
||||||
|
header: string
|
||||||
|
main: string
|
||||||
|
footer: string
|
||||||
|
} = await instance.command.getText()
|
||||||
|
```
|
||||||
|
|||||||
@@ -80,6 +80,7 @@ export class Command {
|
|||||||
public getImage: CommandAdapt['getImage']
|
public getImage: CommandAdapt['getImage']
|
||||||
public getValue: CommandAdapt['getValue']
|
public getValue: CommandAdapt['getValue']
|
||||||
public getHTML: CommandAdapt['getHTML']
|
public getHTML: CommandAdapt['getHTML']
|
||||||
|
public getText: CommandAdapt['getText']
|
||||||
public getWordCount: CommandAdapt['getWordCount']
|
public getWordCount: CommandAdapt['getWordCount']
|
||||||
public getRangeText: CommandAdapt['getRangeText']
|
public getRangeText: CommandAdapt['getRangeText']
|
||||||
public getRangeContext: CommandAdapt['getRangeContext']
|
public getRangeContext: CommandAdapt['getRangeContext']
|
||||||
@@ -174,6 +175,7 @@ export class Command {
|
|||||||
this.getImage = adapt.getImage.bind(adapt)
|
this.getImage = adapt.getImage.bind(adapt)
|
||||||
this.getValue = adapt.getValue.bind(adapt)
|
this.getValue = adapt.getValue.bind(adapt)
|
||||||
this.getHTML = adapt.getHTML.bind(adapt)
|
this.getHTML = adapt.getHTML.bind(adapt)
|
||||||
|
this.getText = adapt.getText.bind(adapt)
|
||||||
this.getWordCount = adapt.getWordCount.bind(adapt)
|
this.getWordCount = adapt.getWordCount.bind(adapt)
|
||||||
this.getRangeText = adapt.getRangeText.bind(adapt)
|
this.getRangeText = adapt.getRangeText.bind(adapt)
|
||||||
this.getRangeContext = adapt.getRangeContext.bind(adapt)
|
this.getRangeContext = adapt.getRangeContext.bind(adapt)
|
||||||
|
|||||||
@@ -29,7 +29,8 @@ import {
|
|||||||
IEditorData,
|
IEditorData,
|
||||||
IEditorHTML,
|
IEditorHTML,
|
||||||
IEditorOption,
|
IEditorOption,
|
||||||
IEditorResult
|
IEditorResult,
|
||||||
|
IEditorText
|
||||||
} from '../../interface/Editor'
|
} from '../../interface/Editor'
|
||||||
import { IElement, IElementStyle } from '../../interface/Element'
|
import { IElement, IElementStyle } from '../../interface/Element'
|
||||||
import { IMargin } from '../../interface/Margin'
|
import { IMargin } from '../../interface/Margin'
|
||||||
@@ -45,7 +46,8 @@ import {
|
|||||||
formatElementList,
|
formatElementList,
|
||||||
isTextLikeElement,
|
isTextLikeElement,
|
||||||
pickElementAttr,
|
pickElementAttr,
|
||||||
getElementListByHTML
|
getElementListByHTML,
|
||||||
|
getTextFromElementList
|
||||||
} from '../../utils/element'
|
} from '../../utils/element'
|
||||||
import { printImageBase64 } from '../../utils/print'
|
import { printImageBase64 } from '../../utils/print'
|
||||||
import { Control } from '../draw/control/Control'
|
import { Control } from '../draw/control/Control'
|
||||||
@@ -1703,6 +1705,17 @@ export class CommandAdapt {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public getText(): IEditorText {
|
||||||
|
const headerElementList = this.draw.getHeaderElementList()
|
||||||
|
const mainElementList = this.draw.getOriginalMainElementList()
|
||||||
|
const footerElementList = this.draw.getFooterElementList()
|
||||||
|
return {
|
||||||
|
header: getTextFromElementList(headerElementList),
|
||||||
|
main: getTextFromElementList(mainElementList),
|
||||||
|
footer: getTextFromElementList(footerElementList)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public getWordCount(): Promise<number> {
|
public getWordCount(): Promise<number> {
|
||||||
return this.workerManager.getWordCount()
|
return this.workerManager.getWordCount()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -509,7 +509,7 @@ export class RangeManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public toString(): string {
|
public toString(): string {
|
||||||
const selection = this.getSelection()
|
const selection = this.getTextLikeSelection()
|
||||||
if (!selection) return ''
|
if (!selection) return ''
|
||||||
return selection
|
return selection
|
||||||
.map(s => s.value)
|
.map(s => s.value)
|
||||||
|
|||||||
@@ -86,3 +86,5 @@ export interface IEditorHTML {
|
|||||||
main: string
|
main: string
|
||||||
footer: string
|
footer: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type IEditorText = IEditorHTML
|
||||||
|
|||||||
+87
-24
@@ -701,6 +701,36 @@ export function convertElementToDom(
|
|||||||
return dom
|
return dom
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function splitListElement(
|
||||||
|
elementList: IElement[]
|
||||||
|
): Map<number, IElement[]> {
|
||||||
|
let curListIndex = 0
|
||||||
|
const listElementListMap: Map<number, IElement[]> = new Map()
|
||||||
|
for (let e = 0; e < elementList.length; e++) {
|
||||||
|
const element = elementList[e]
|
||||||
|
if (element.listWrap) {
|
||||||
|
const listElementList = listElementListMap.get(curListIndex) || []
|
||||||
|
listElementList.push(element)
|
||||||
|
listElementListMap.set(curListIndex, listElementList)
|
||||||
|
} else {
|
||||||
|
const valueList = element.value.split('\n')
|
||||||
|
for (let c = 0; c < valueList.length; c++) {
|
||||||
|
if (c > 0) {
|
||||||
|
curListIndex += 1
|
||||||
|
}
|
||||||
|
const value = valueList[c]
|
||||||
|
const listElementList = listElementListMap.get(curListIndex) || []
|
||||||
|
listElementList.push({
|
||||||
|
...element,
|
||||||
|
value
|
||||||
|
})
|
||||||
|
listElementListMap.set(curListIndex, listElementList)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return listElementListMap
|
||||||
|
}
|
||||||
|
|
||||||
export function createDomFromElementList(
|
export function createDomFromElementList(
|
||||||
elementList: IElement[],
|
elementList: IElement[],
|
||||||
options: DeepRequired<IEditorOption>
|
options: DeepRequired<IEditorOption>
|
||||||
@@ -754,31 +784,8 @@ export function createDomFromElementList(
|
|||||||
list.style.listStyleType = listStyleCSSMapping[element.listStyle]
|
list.style.listStyleType = listStyleCSSMapping[element.listStyle]
|
||||||
}
|
}
|
||||||
// 按照换行符拆分
|
// 按照换行符拆分
|
||||||
let curListIndex = 0
|
|
||||||
const listElementListMap: Map<number, IElement[]> = new Map()
|
|
||||||
const zipList = zipElementList(element.valueList!)
|
const zipList = zipElementList(element.valueList!)
|
||||||
for (let z = 0; z < zipList.length; z++) {
|
const listElementListMap = splitListElement(zipList)
|
||||||
const zipElement = zipList[z]
|
|
||||||
if (zipElement.listWrap) {
|
|
||||||
const listElementList = listElementListMap.get(curListIndex) || []
|
|
||||||
listElementList.push(zipElement)
|
|
||||||
listElementListMap.set(curListIndex, listElementList)
|
|
||||||
} else {
|
|
||||||
const zipValueList = zipElement.value.split('\n')
|
|
||||||
for (let c = 0; c < zipValueList.length; c++) {
|
|
||||||
if (c > 0) {
|
|
||||||
curListIndex += 1
|
|
||||||
}
|
|
||||||
const value = zipValueList[c]
|
|
||||||
const listElementList = listElementListMap.get(curListIndex) || []
|
|
||||||
listElementList.push({
|
|
||||||
...zipElement,
|
|
||||||
value
|
|
||||||
})
|
|
||||||
listElementListMap.set(curListIndex, listElementList)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
listElementListMap.forEach(listElementList => {
|
listElementListMap.forEach(listElementList => {
|
||||||
const li = document.createElement('li')
|
const li = document.createElement('li')
|
||||||
const childDom = buildDom(listElementList)
|
const childDom = buildDom(listElementList)
|
||||||
@@ -1061,3 +1068,59 @@ export function getElementListByHTML(
|
|||||||
clipboardDom.remove()
|
clipboardDom.remove()
|
||||||
return elementList
|
return elementList
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getTextFromElementList(elementList: IElement[]) {
|
||||||
|
function buildText(payload: IElement[]): string {
|
||||||
|
let text = ''
|
||||||
|
for (let e = 0; e < payload.length; e++) {
|
||||||
|
const element = payload[e]
|
||||||
|
// 构造表格
|
||||||
|
if (element.type === ElementType.TABLE) {
|
||||||
|
text += `\n`
|
||||||
|
const trList = element.trList!
|
||||||
|
for (let t = 0; t < trList.length; t++) {
|
||||||
|
const tr = trList[t]
|
||||||
|
for (let d = 0; d < tr.tdList.length; d++) {
|
||||||
|
const td = tr.tdList[d]
|
||||||
|
const tdText = buildText(zipElementList(td.value!))
|
||||||
|
const isFirst = d === 0
|
||||||
|
const isLast = tr.tdList.length - 1 === d
|
||||||
|
text += `${!isFirst ? ` ` : ``}${tdText}${isLast ? `\n` : ``}`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (element.type === ElementType.HYPERLINK) {
|
||||||
|
text += element.valueList!.map(v => v.value).join('')
|
||||||
|
} else if (element.type === ElementType.TITLE) {
|
||||||
|
text += `${buildText(zipElementList(element.valueList!))}`
|
||||||
|
} else if (element.type === ElementType.LIST) {
|
||||||
|
// 按照换行符拆分
|
||||||
|
const zipList = zipElementList(element.valueList!)
|
||||||
|
const listElementListMap = splitListElement(zipList)
|
||||||
|
listElementListMap.forEach((listElementList, listIndex) => {
|
||||||
|
const isLast = listElementListMap.size - 1 === listIndex
|
||||||
|
text += `\n${listIndex + 1}.${buildText(listElementList)}${
|
||||||
|
isLast ? `\n` : ``
|
||||||
|
}`
|
||||||
|
})
|
||||||
|
} else if (element.type === ElementType.CHECKBOX) {
|
||||||
|
text += element.checkbox?.value ? `☑` : `□`
|
||||||
|
} else if (
|
||||||
|
!element.type ||
|
||||||
|
element.type === ElementType.LATEX ||
|
||||||
|
TEXTLIKE_ELEMENT_TYPE.includes(element.type)
|
||||||
|
) {
|
||||||
|
let textLike = ''
|
||||||
|
if (element.type === ElementType.CONTROL) {
|
||||||
|
textLike = element.control!.value?.[0]?.value || ''
|
||||||
|
} else if (element.type === ElementType.DATE) {
|
||||||
|
textLike = element.valueList?.map(v => v.value).join('') || ''
|
||||||
|
} else {
|
||||||
|
textLike = element.value
|
||||||
|
}
|
||||||
|
text += textLike.replace(new RegExp(`${ZERO}`, 'g'), '\n')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return text
|
||||||
|
}
|
||||||
|
return buildText(zipElementList(elementList))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user