MCPcopy Create free account
hub / github.com/OpenAnonymity/oa-chat / extractTextFromEditableDiff

Function extractTextFromEditableDiff

chat/services/editableDiffRenderer.js:537–581  ·  view source on GitHub ↗
(container)

Source from the content-addressed store, hash-verified

535};
536
537export const extractTextFromEditableDiff = (container) => {
538 if (!container) return '';
539 const clone = container.cloneNode(true);
540 clone.querySelectorAll('.scrubber-deleted').forEach((node) => node.remove());
541
542 const output = [];
543 const blockTags = new Set(['DIV', 'P']);
544
545 const appendNodeText = (node) => {
546 if (node.nodeType === Node.TEXT_NODE) {
547 output.push(node.nodeValue || '');
548 return;
549 }
550 if (node.nodeType !== Node.ELEMENT_NODE) return;
551
552 const element = node;
553 if (element.tagName === 'BR') {
554 output.push('\n');
555 return;
556 }
557
558 Array.from(element.childNodes).forEach(appendNodeText);
559 };
560
561 const children = Array.from(clone.childNodes);
562 children.forEach((child, index) => {
563 appendNodeText(child);
564 if (
565 child.nodeType === Node.ELEMENT_NODE &&
566 blockTags.has(child.tagName) &&
567 index < children.length - 1
568 ) {
569 output.push('\n');
570 }
571 });
572
573 let text = output.join('').replace(/\u00a0/g, ' ').replace(/\u200B/g, '');
574 text = text.replace(/\r\n/g, '\n');
575 // When content is deleted, browsers leave behind <br> elements that become trailing newlines.
576 // If the result is only whitespace/newlines, return empty string.
577 if (text.trim() === '') {
578 return '';
579 }
580 return text;
581};
582
583const insertTextAtCursor = (text) => {
584 if (!text) return;

Callers 1

renderScrubberPreviewMethod · 0.90

Calls 3

appendNodeTextFunction · 0.85
pushMethod · 0.80
removeMethod · 0.45

Tested by

no test coverage detected