Skip to content

Commit

Permalink
feat: add getText api #240
Browse files Browse the repository at this point in the history
  • Loading branch information
Hufe921 committed Aug 11, 2023
1 parent fd31b3e commit f8fdea6
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 27 deletions.
14 changes: 14 additions & 0 deletions docs/en/guide/command-get.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,17 @@ const {
footer: string
} = await instance.command.getHTML()
```

## getText

Feature: Get text

Usage:

```javascript
const {
header: string
main: string
footer: string
} = await instance.command.getText()
```
14 changes: 14 additions & 0 deletions docs/guide/command-get.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,17 @@ const {
footer: string
} = await instance.command.getHTML()
```

## getText

功能:获取文本

用法:

```javascript
const {
header: string
main: string
footer: string
} = await instance.command.getText()
```
2 changes: 2 additions & 0 deletions src/editor/core/command/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export class Command {
public getImage: CommandAdapt['getImage']
public getValue: CommandAdapt['getValue']
public getHTML: CommandAdapt['getHTML']
public getText: CommandAdapt['getText']
public getWordCount: CommandAdapt['getWordCount']
public getRangeText: CommandAdapt['getRangeText']
public getRangeContext: CommandAdapt['getRangeContext']
Expand Down Expand Up @@ -174,6 +175,7 @@ export class Command {
this.getImage = adapt.getImage.bind(adapt)
this.getValue = adapt.getValue.bind(adapt)
this.getHTML = adapt.getHTML.bind(adapt)
this.getText = adapt.getText.bind(adapt)
this.getWordCount = adapt.getWordCount.bind(adapt)
this.getRangeText = adapt.getRangeText.bind(adapt)
this.getRangeContext = adapt.getRangeContext.bind(adapt)
Expand Down
17 changes: 15 additions & 2 deletions src/editor/core/command/CommandAdapt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import {
IEditorData,
IEditorHTML,
IEditorOption,
IEditorResult
IEditorResult,
IEditorText
} from '../../interface/Editor'
import { IElement, IElementStyle } from '../../interface/Element'
import { IMargin } from '../../interface/Margin'
Expand All @@ -45,7 +46,8 @@ import {
formatElementList,
isTextLikeElement,
pickElementAttr,
getElementListByHTML
getElementListByHTML,
getTextFromElementList
} from '../../utils/element'
import { printImageBase64 } from '../../utils/print'
import { Control } from '../draw/control/Control'
Expand Down Expand Up @@ -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> {
return this.workerManager.getWordCount()
}
Expand Down
2 changes: 1 addition & 1 deletion src/editor/core/range/RangeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ export class RangeManager {
}

public toString(): string {
const selection = this.getSelection()
const selection = this.getTextLikeSelection()
if (!selection) return ''
return selection
.map(s => s.value)
Expand Down
2 changes: 2 additions & 0 deletions src/editor/interface/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,5 @@ export interface IEditorHTML {
main: string
footer: string
}

export type IEditorText = IEditorHTML
111 changes: 87 additions & 24 deletions src/editor/utils/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,36 @@ export function convertElementToDom(
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(
elementList: IElement[],
options: DeepRequired<IEditorOption>
Expand Down Expand Up @@ -754,31 +784,8 @@ export function createDomFromElementList(
list.style.listStyleType = listStyleCSSMapping[element.listStyle]
}
// 按照换行符拆分
let curListIndex = 0
const listElementListMap: Map<number, IElement[]> = new Map()
const zipList = zipElementList(element.valueList!)
for (let z = 0; z < zipList.length; z++) {
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)
}
}
}
const listElementListMap = splitListElement(zipList)
listElementListMap.forEach(listElementList => {
const li = document.createElement('li')
const childDom = buildDom(listElementList)
Expand Down Expand Up @@ -1061,3 +1068,59 @@ export function getElementListByHTML(
clipboardDom.remove()
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))
}

0 comments on commit f8fdea6

Please sign in to comment.