From 19920deeda32af804c805549bd8fcb21e4a43aca Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Thu, 22 Mar 2018 18:03:13 +0100 Subject: [PATCH] Better detect tag ancestors --- .../client/components/Editor.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/plugins/talk-plugin-rich-text/client/components/Editor.js b/plugins/talk-plugin-rich-text/client/components/Editor.js index 5c1497f00..eefb978a8 100644 --- a/plugins/talk-plugin-rich-text/client/components/Editor.js +++ b/plugins/talk-plugin-rich-text/client/components/Editor.js @@ -46,13 +46,17 @@ class Editor extends React.Component { this.props.unregisterHook(this.normalizeHook); } - getCurrentTagName() { + hasAncestor(tag) { const sel = window.getSelection(); const range = sel.getRangeAt(0); - if (range.startContainer.nodeName !== '#text') { - return range.startContainer.nodeName; - } - return range.startContainer.parentNode.tagName; + let cur = range.startContainer; + do { + if (cur.nodeName === tag) { + return true; + } + cur = cur.parentNode; + } while (cur); + return false; } formatBold = () => { @@ -66,8 +70,7 @@ class Editor extends React.Component { }; formatBlockquote = () => { - const currentTag = this.getCurrentTagName(); - if (currentTag === 'BLOCKQUOTE') { + if (this.hasAncestor('BLOCKQUOTE')) { document.execCommand('outdent'); } else { if (bowser.msie) {