Skip to content

Commit

Permalink
Merge pull request #306 from FrancisMengx/change-getTextContent
Browse files Browse the repository at this point in the history
Add getTextContent
  • Loading branch information
FrancisMengx authored Jun 6, 2019
2 parents 5834321 + 90628f9 commit 1a667bf
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 9 deletions.
15 changes: 8 additions & 7 deletions packages/roosterjs-editor-core/lib/editor/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
findClosestElementAncestor,
fromHtml,
getBlockElementAtNode,
getTextContent,
getInlineElementAtNode,
getPositionRect,
getRangeFromSelectionPath,
Expand Down Expand Up @@ -104,10 +105,10 @@ export default class Editor {
if (Browser.isFirefox) {
this.core.document.execCommand(DocumentCommand.EnableObjectResizing, false, <
string
>(<any>false));
>(<any>false));
this.core.document.execCommand(DocumentCommand.EnableInlineTableEditing, false, <
string
>(<any>false));
>(<any>false));
} else if (Browser.isIE) {
// Change the default paragraph separater to DIV. This is mainly for IE since its default setting is P
this.core.document.execCommand(
Expand All @@ -116,7 +117,7 @@ export default class Editor {
'div'
);
}
} catch (e) {}
} catch (e) { }

// 9. Let plugins know that we are ready
this.triggerEvent(
Expand Down Expand Up @@ -385,7 +386,7 @@ export default class Editor {
* @returns The text content inside editor
*/
public getTextContent(): string {
return this.core.contentDiv.innerText;
return getTextContent(this.core.contentDiv);
}

/**
Expand All @@ -406,7 +407,7 @@ export default class Editor {
this.deleteNode(pathComment);
let range = getRangeFromSelectionPath(contentDiv, path);
this.select(range);
} catch {}
} catch { }
}

if (triggerContentChangedEvent) {
Expand Down Expand Up @@ -621,8 +622,8 @@ export default class Editor {
nameOrMap:
| string
| {
[eventName: string]: (event: UIEvent) => void;
},
[eventName: string]: (event: UIEvent) => void;
},
handler?: (event: UIEvent) => void
): () => void {
if (nameOrMap instanceof Object) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { BlockElement } from 'roosterjs-editor-types';
* In most cases, it corresponds to an HTML block level element, i.e. P, DIV, LI, TD etc.
*/
export default class NodeBlockElement implements BlockElement {
constructor(private element: HTMLElement) {}
constructor(private element: HTMLElement) { }

/**
* Collapse this element to a single DOM element.
Expand Down Expand Up @@ -56,4 +56,11 @@ export default class NodeBlockElement implements BlockElement {
public contains(node: Node): boolean {
return contains(this.element, node, true /*treatSameNodeAsContain*/);
}

/**
* Get the text content of this block element
*/
public getTextContent(): string {
return this.element.textContent;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import isNodeAfter from '../utils/isNodeAfter';
import wrap from '../utils/wrap';
import { BlockElement } from 'roosterjs-editor-types';
import { splitBalancedNodeRange } from '../utils/splitParentNode';
import createRange from '../selection/createRange';

const STRUCTURE_NODE_TAGS = ['TD', 'TH', 'LI', 'BLOCKQUOTE'];

Expand All @@ -18,7 +19,7 @@ const STRUCTURE_NODE_TAGS = ['TD', 'TH', 'LI', 'BLOCKQUOTE'];
* This start and end must be in same sibling level and have same parent in DOM tree
*/
export default class StartEndBlockElement implements BlockElement {
constructor(private rootNode: Node, private startNode: Node, private endNode: Node) {}
constructor(private rootNode: Node, private startNode: Node, private endNode: Node) { }

static getBlockContext(node: Node): HTMLElement {
while (node && !isBlockElement(node)) {
Expand Down Expand Up @@ -94,4 +95,11 @@ export default class StartEndBlockElement implements BlockElement {
(isNodeAfter(node, this.startNode) && isNodeAfter(this.endNode, node))
);
}

/**
* Get the text content of this block element
*/
public getTextContent(): string {
return createRange(this.getStartNode(), this.getEndNode()).toString();
}
}
1 change: 1 addition & 0 deletions packages/roosterjs-editor-dom/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export { default as unwrap } from './utils/unwrap';
export { default as wrap } from './utils/wrap';
export { getNextLeafSibling, getPreviousLeafSibling } from './utils/getLeafSibling';
export { getFirstLeafNode, getLastLeafNode } from './utils/getLeafNode';
export { default as getTextContent } from './utils/getTextContent';

export { default as VTable, VCell } from './table/VTable';

Expand Down
19 changes: 19 additions & 0 deletions packages/roosterjs-editor-dom/lib/utils/getTextContent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import ContentTraverser from '../contentTraverser/ContentTraverser';

/**
* get block element's text content.
* @param rootNode Root node that the get the textContent of.
* @returns text content of given text content.
*/
export default function getTextContent(rootNode: Node): string {
const traverser = ContentTraverser.createBodyTraverser(rootNode);
let block = traverser && traverser.currentBlockElement;
let textContent: string[] = [];

while (block) {
textContent.push(block.getTextContent());
block = traverser.getNextBlockElement();
}

return textContent.join('\n');
}
5 changes: 5 additions & 0 deletions packages/roosterjs-editor-types/lib/interface/BlockElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ export default interface BlockElement {
* Check if the given node is within this block element
*/
contains(node: Node): boolean;

/**
* Get the text content of this block element
*/
getTextContent(): string;
}

0 comments on commit 1a667bf

Please sign in to comment.