fix: Emojis and embeds cannot be copied to plain text clipboard (#3561)

This commit is contained in:
Tom Moor
2022-05-20 09:47:13 -07:00
committed by GitHub
parent 90ca8655af
commit ba7b3fff05
10 changed files with 99 additions and 4 deletions

View File

@@ -0,0 +1,43 @@
import { Node as ProseMirrorNode } from "prosemirror-model";
import { PlainTextSerializer } from "../types";
/**
* Returns the text content between two positions.
*
* @param doc The Prosemirror document to use
* @param from A start point
* @param to An end point
* @param plainTextSerializers A map of node names to PlainTextSerializers which convert a node to plain text
* @returns A string of plain text
*/
export default function textBetween(
doc: ProseMirrorNode,
from: number,
to: number,
plainTextSerializers: Record<string, PlainTextSerializer | undefined>
): string {
const blockSeparator = "\n\n";
let text = "";
let separated = true;
doc.nodesBetween(from, to, (node, pos) => {
const toPlainText = plainTextSerializers[node.type.name];
if (toPlainText) {
if (node.isBlock && !separated) {
text += blockSeparator;
separated = true;
}
text += toPlainText(node);
} else if (node.isText) {
text += node.text?.slice(Math.max(from, pos) - pos, to - pos);
separated = false;
} else if (node.isBlock && !separated) {
text += blockSeparator;
separated = true;
}
});
return text;
}