less duplicate code
This commit is contained in:
parent
ba97bfdd9e
commit
61149b458a
@ -109,14 +109,8 @@ function minimizeAttributeChanges (left, right, currentAttributes, attributes) {
|
|||||||
return [left, right]
|
return [left, right]
|
||||||
}
|
}
|
||||||
|
|
||||||
function insertText (y, text, parent, left, right, currentAttributes, attributes) {
|
function insertAttributes (y, parent, left, right, attributes, currentAttributes) {
|
||||||
for (let [key] of currentAttributes) {
|
const negatedAttributes = new Map()
|
||||||
if (attributes.hasOwnProperty(key) === false) {
|
|
||||||
attributes[key] = null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
[left, right] = minimizeAttributeChanges(left, right, currentAttributes, attributes)
|
|
||||||
let negatedAttributes = new Map()
|
|
||||||
// insert format-start items
|
// insert format-start items
|
||||||
for (let key in attributes) {
|
for (let key in attributes) {
|
||||||
const val = attributes[key]
|
const val = attributes[key]
|
||||||
@ -131,6 +125,17 @@ function insertText (y, text, parent, left, right, currentAttributes, attributes
|
|||||||
left = format
|
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
|
// insert content
|
||||||
let item
|
let item
|
||||||
if (text.constructor === String) {
|
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) {
|
function formatText (y, length, parent, left, right, currentAttributes, attributes) {
|
||||||
[left, right] = minimizeAttributeChanges(left, right, currentAttributes, attributes)
|
[left, right] = minimizeAttributeChanges(left, right, currentAttributes, attributes)
|
||||||
let negatedAttributes = new Map()
|
const negatedAttributes = insertAttributes(y, parent, left, right, attributes, currentAttributes)
|
||||||
// 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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// iterate until first non-format or null is found
|
// iterate until first non-format or null is found
|
||||||
// delete all formats with attributes[format.key] != null
|
// delete all formats with attributes[format.key] != null
|
||||||
while (length > 0 && right !== null) {
|
while (length > 0 && right !== null) {
|
||||||
|
@ -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 {
|
class N {
|
||||||
// A created node is always red!
|
// A created node is always red!
|
||||||
constructor (val) {
|
constructor (val) {
|
||||||
@ -41,21 +54,12 @@ class N {
|
|||||||
this._right = n
|
this._right = n
|
||||||
}
|
}
|
||||||
rotateLeft (tree) {
|
rotateLeft (tree) {
|
||||||
var parent = this.parent
|
const parent = this.parent
|
||||||
var newParent = this.right
|
const newParent = this.right
|
||||||
var newRight = this.right.left
|
const newRight = this.right.left
|
||||||
newParent.left = this
|
newParent.left = this
|
||||||
this.right = newRight
|
this.right = newRight
|
||||||
if (parent === null) {
|
rotate(tree, parent, newParent, this)
|
||||||
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!')
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
next () {
|
next () {
|
||||||
if (this.right !== null) {
|
if (this.right !== null) {
|
||||||
@ -90,21 +94,12 @@ class N {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
rotateRight (tree) {
|
rotateRight (tree) {
|
||||||
var parent = this.parent
|
const parent = this.parent
|
||||||
var newParent = this.left
|
const newParent = this.left
|
||||||
var newLeft = this.left.right
|
const newLeft = this.left.right
|
||||||
newParent.right = this
|
newParent.right = this
|
||||||
this.left = newLeft
|
this.left = newLeft
|
||||||
if (parent === null) {
|
rotate(tree, parent, newParent, this)
|
||||||
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!')
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
getUncle () {
|
getUncle () {
|
||||||
// we can assume that grandparent exists when this is called!
|
// we can assume that grandparent exists when this is called!
|
||||||
|
@ -31,24 +31,6 @@ export default class YEvent {
|
|||||||
* type === event.target // => true
|
* type === event.target // => true
|
||||||
*/
|
*/
|
||||||
get path () {
|
get path () {
|
||||||
const path = []
|
return this.currentTarget.getPathTo(this.target)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user