MCPcopy
hub / github.com/codex-team/editor.js / leafNodesAndReturnIndex

Method leafNodesAndReturnIndex

src/components/domIterator.ts:117–191  ·  view source on GitHub ↗

* Leafs nodes inside the target list from active element * * @param {string} direction - leaf direction. Can be 'left' or 'right' * @returns {number} index of focused node

(direction: string)

Source from the content-addressed store, hash-verified

115 * @returns {number} index of focused node
116 */
117 private leafNodesAndReturnIndex(direction: string): number {
118 /**
119 * if items are empty then there is nothing to leaf
120 */
121 if (this.items.length === 0) {
122 return this.cursor;
123 }
124
125 let focusedButtonIndex = this.cursor;
126
127 /**
128 * If activeButtonIndex === -1 then we have no chosen Tool in Toolbox
129 */
130 if (focusedButtonIndex === -1) {
131 /**
132 * Normalize "previous" Tool index depending on direction.
133 * We need to do this to highlight "first" Tool correctly
134 *
135 * Order of Tools: [0] [1] ... [n - 1]
136 * [0 = n] because of: n % n = 0 % n
137 *
138 * Direction 'right': for [0] the [n - 1] is a previous index
139 * [n - 1] -> [0]
140 *
141 * Direction 'left': for [n - 1] the [0] is a previous index
142 * [n - 1] <- [0]
143 *
144 * @type {number}
145 */
146 focusedButtonIndex = direction === DomIterator.directions.RIGHT ? -1 : 0;
147 } else {
148 /**
149 * If we have chosen Tool then remove highlighting
150 */
151 this.items[focusedButtonIndex].classList.remove(this.focusedCssClass);
152 }
153
154 /**
155 * Count index for next Tool
156 */
157 if (direction === DomIterator.directions.RIGHT) {
158 /**
159 * If we go right then choose next (+1) Tool
160 *
161 * @type {number}
162 */
163 focusedButtonIndex = (focusedButtonIndex + 1) % this.items.length;
164 } else {
165 /**
166 * If we go left then choose previous (-1) Tool
167 * Before counting module we need to add length before because of "The JavaScript Modulo Bug"
168 *
169 * @type {number}
170 */
171 focusedButtonIndex = (this.items.length + focusedButtonIndex - 1) % this.items.length;
172 }
173
174 if (Dom.canSetCaret(this.items[focusedButtonIndex])) {

Callers 2

nextMethod · 0.95
previousMethod · 0.95

Calls 4

canSetCaretMethod · 0.80
removeMethod · 0.45
setCursorMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected