From 6489680c57700c1e599a0f9e60655bdcdb0e12a6 Mon Sep 17 00:00:00 2001 From: void Date: Fri, 12 Feb 2021 19:10:55 +0000 Subject: [PATCH] WIP: arreglar marcado de cosas entre varios elementos --- app/javascript/editor/types/marks.ts | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/app/javascript/editor/types/marks.ts b/app/javascript/editor/types/marks.ts index 560f3f6..04e9805 100644 --- a/app/javascript/editor/types/marks.ts +++ b/app/javascript/editor/types/marks.ts @@ -52,10 +52,14 @@ export function setupButtons (editor: Editor): void { const range = safeGetRangeAt(sel) if (!range) return - if (!(range.commonAncestorContainer instanceof Element)) - throw new Error(':/') + let parentEl = range.commonAncestorContainer + while (!(parentEl instanceof Element)) { + if (!parentEl.parentElement) return + parentEl = parentEl.parentElement + } + const existingMarks = recursiveFilterSelection( - range.commonAncestorContainer, + parentEl, sel, type.selector, ) @@ -68,14 +72,13 @@ export function setupButtons (editor: Editor): void { moveChildren(mark, mark.parentElement, mark) mark.parentElement.removeChild(mark) } else { - const tagEl = type.create() - - try { - range.surroundContents(tagEl) - } catch (error) { + if (range.commonAncestorContainer === editor.contentEl) // TODO: mostrar error return console.error("No puedo marcar cosas a través de distintos bloques!") - } + + const tagEl = type.create() + + tagEl.appendChild(range.extractContents()) range.insertNode(tagEl) range.selectNode(tagEl)