From 61149b458ac15837511a029316f058d87673d8ae Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Fri, 23 Mar 2018 05:22:45 +0100 Subject: [PATCH] less duplicate code --- src/Types/YText/YText.js | 37 ++++++++++++------------------- src/Util/Tree.js | 47 ++++++++++++++++++---------------------- src/Util/YEvent.js | 20 +---------------- 3 files changed, 36 insertions(+), 68 deletions(-) diff --git a/src/Types/YText/YText.js b/src/Types/YText/YText.js index ff1e764b..b75931fb 100644 --- a/src/Types/YText/YText.js +++ b/src/Types/YText/YText.js @@ -109,14 +109,8 @@ function minimizeAttributeChanges (left, right, currentAttributes, attributes) { return [left, right] } -function insertText (y, text, parent, left, right, currentAttributes, attributes) { - for (let [key] of currentAttributes) { - if (attributes.hasOwnProperty(key) === false) { - attributes[key] = null - } - } - [left, right] = minimizeAttributeChanges(left, right, currentAttributes, attributes) - let negatedAttributes = new Map() +function insertAttributes (y, parent, left, right, attributes, currentAttributes) { + const negatedAttributes = new Map() // insert format-start items for (let key in attributes) { const val = attributes[key] @@ -131,6 +125,17 @@ function insertText (y, text, parent, left, right, currentAttributes, attributes left = format } } + return negatedAttributes +} + +function insertText (y, text, parent, left, right, currentAttributes, attributes) { + for (let [key] of currentAttributes) { + if (attributes.hasOwnProperty(key) === false) { + attributes[key] = null + } + } + [left, right] = minimizeAttributeChanges(left, right, currentAttributes, attributes) + const negatedAttributes = insertAttributes(y, parent, left, right, attributes, currentAttributes) // insert content let item if (text.constructor === String) { @@ -147,21 +152,7 @@ function insertText (y, text, parent, left, right, currentAttributes, attributes function formatText (y, length, parent, left, right, currentAttributes, attributes) { [left, right] = minimizeAttributeChanges(left, right, currentAttributes, attributes) - let negatedAttributes = new Map() - // insert format-start items - for (let key in attributes) { - const val = attributes[key] - const currentVal = currentAttributes.get(key) - if (currentVal !== val) { - // save negated attribute (set null if currentVal undefined) - negatedAttributes.set(key, currentVal || null) - let format = new ItemFormat() - format.key = key - format.value = val - integrateItem(format, parent, y, left, right) - left = format - } - } + const negatedAttributes = insertAttributes(y, parent, left, right, attributes, currentAttributes) // iterate until first non-format or null is found // delete all formats with attributes[format.key] != null while (length > 0 && right !== null) { diff --git a/src/Util/Tree.js b/src/Util/Tree.js index d39060a3..4f15e618 100644 --- a/src/Util/Tree.js +++ b/src/Util/Tree.js @@ -1,4 +1,17 @@ +function rotate (tree, parent, newParent, n) { + if (parent === null) { + tree.root = newParent + newParent._parent = null + } else if (parent.left === n) { + parent.left = newParent + } else if (parent.right === n) { + parent.right = newParent + } else { + throw new Error('The elements are wrongly connected!') + } +} + class N { // A created node is always red! constructor (val) { @@ -41,21 +54,12 @@ class N { this._right = n } rotateLeft (tree) { - var parent = this.parent - var newParent = this.right - var newRight = this.right.left + const parent = this.parent + const newParent = this.right + const newRight = this.right.left newParent.left = this this.right = newRight - if (parent === null) { - tree.root = newParent - newParent._parent = null - } else if (parent.left === this) { - parent.left = newParent - } else if (parent.right === this) { - parent.right = newParent - } else { - throw new Error('The elements are wrongly connected!') - } + rotate(tree, parent, newParent, this) } next () { if (this.right !== null) { @@ -90,21 +94,12 @@ class N { } } rotateRight (tree) { - var parent = this.parent - var newParent = this.left - var newLeft = this.left.right + const parent = this.parent + const newParent = this.left + const newLeft = this.left.right newParent.right = this this.left = newLeft - if (parent === null) { - tree.root = newParent - newParent._parent = null - } else if (parent.left === this) { - parent.left = newParent - } else if (parent.right === this) { - parent.right = newParent - } else { - throw new Error('The elements are wrongly connected!') - } + rotate(tree, parent, newParent, this) } getUncle () { // we can assume that grandparent exists when this is called! diff --git a/src/Util/YEvent.js b/src/Util/YEvent.js index b9096db9..0775527f 100644 --- a/src/Util/YEvent.js +++ b/src/Util/YEvent.js @@ -31,24 +31,6 @@ export default class YEvent { * type === event.target // => true */ get path () { - const path = [] - let type = this.target - const y = type._y - while (type !== this.currentTarget && type !== y) { - let parent = type._parent - if (type._parentSub !== null) { - path.unshift(type._parentSub) - } else { - // parent is array-ish - for (let [i, child] of parent) { - if (child === type) { - path.unshift(i) - break - } - } - } - type = parent - } - return path + return this.currentTarget.getPathTo(this.target) } }