Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paste handler: optimization and unit test #220

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 50 additions & 61 deletions blocks/edit/prose/plugins/sectionPasteHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,24 @@ function closeParagraph(paraContent, newContent) {
* In Desktop Word each section is represented as a top-level div element, right
* under the body element.
*/
function handleDesktopWordSectionBreaks(html) {
try {
const parser = new DOMParser();
const doc = parser.parseFromString(html, 'text/html');

if (doc.querySelector('meta[name="ProgId"]')?.content !== 'Word.Document') {
// This is not a word document
return html;
}

let modified = false;
// Add a hr element after all top-level div elements
const sections = doc.querySelectorAll('body > div');
sections.forEach((section) => {
if (section.nextElementSibling) {
// only add the hr if there is something after the section
section.after(doc.createElement('hr'));
modified = true;
}
});
function handleDesktopWordSectionBreaks(doc) {
if (doc.querySelector('meta[name="ProgId"]')?.content !== 'Word.Document') {
// This is not a word document
return false;
}

if (!modified) {
return html;
let modified = false;
// Add a hr element after all top-level div elements
const sections = doc.querySelectorAll('body > div');
sections.forEach((section) => {
if (section.nextElementSibling) {
// only add the hr if there is something after the section
section.after(doc.createElement('hr'));
modified = true;
}
});

const serializer = new XMLSerializer();
return serializer.serializeToString(doc);
} catch (error) {
// eslint-disable-next-line no-console
console.error('Error handling desktop Word section breaks:', error);
return html;
}
return modified;
}

/**
Expand All @@ -60,38 +46,24 @@ function handleDesktopWordSectionBreaks(html) {
* to be the only way to find them. In the future Word online might provide a
* better way to identify section breaks.
*/
function handleWordOnlineSectionBreaks(html) {
try {
const parser = new DOMParser();
const doc = parser.parseFromString(html, 'text/html');

let modified = false;
// The span[data-ccp-props] are the magic indicator if one of the JSON values in there is the
// word 'single' then we need to add a section break.
const sections = doc.querySelectorAll('div > p > span[data-ccp-props]');
sections.forEach((section) => {
const props = JSON.parse(section.getAttribute('data-ccp-props'));
for (const key of Object.keys(props)) {
if (props[key] === 'single') {
const hr = doc.createElement('hr');
section.parentNode.after(hr);
modified = true;
break;
}
function handleWordOnlineSectionBreaks(doc) {
let modified = false;
// The span[data-ccp-props] are the magic indicator if one of the JSON values in there is the
// word 'single' then we need to add a section break.
const sections = doc.querySelectorAll('div > p > span[data-ccp-props]');
sections.forEach((section) => {
const props = JSON.parse(section.getAttribute('data-ccp-props'));
for (const key of Object.keys(props)) {
if (props[key] === 'single') {
const hr = doc.createElement('hr');
section.parentNode.after(hr);
modified = true;
break;
}
});

if (!modified) {
return html;
}
});

const serializer = new XMLSerializer();
return serializer.serializeToString(doc);
} catch (error) {
// eslint-disable-next-line no-console
console.error('Error handling Word online section breaks:', error);
return html;
}
return modified;
}

/* When text is pasted, handle section breaks. */
Expand All @@ -103,9 +75,26 @@ export default function sectionPasteHandler(schema) {
* these section breaks and adds a <hr/> element for them.
*/
transformPastedHTML: (html) => {
const newHTML = handleDesktopWordSectionBreaks(html);
const newHTML2 = handleWordOnlineSectionBreaks(newHTML);
return newHTML2;
try {
const parser = new DOMParser();
const doc = parser.parseFromString(html, 'text/html');

let modified = handleDesktopWordSectionBreaks(doc);
if (!modified) {
modified = handleWordOnlineSectionBreaks(doc);
}

if (!modified) {
return html;
}

const serializer = new XMLSerializer();
return serializer.serializeToString(doc);
} catch (error) {
// eslint-disable-next-line no-console
console.error('Error handling Word section breaks:', error);
return html;
}
},

/* Convert 3 dashes on a line by itself (top level only) to a horizontal rule,
Expand Down
Loading
Loading