Skip to content

Commit ec363ea

Browse files
committed
fix: make drawToken a free function
1 parent a296f48 commit ec363ea

File tree

1 file changed

+45
-44
lines changed

1 file changed

+45
-44
lines changed

lib/mixins/canvas-drawer.js

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -434,56 +434,14 @@ export default class CanvasDrawer extends Mixin {
434434
? this.getTokenColor(token)
435435
: this.getDefaultColor()
436436

437-
x = this.drawToken(
438-
context, token.text, color, x, y, charWidth, charHeight
437+
x = drawToken(
438+
context, token.text, color, x, y, charWidth, charHeight, this.ignoreWhitespacesInTokens
439439
)
440440
}
441441
})
442442
context.fill()
443443
}
444444

445-
/**
446-
* Draws a single token on the given context.
447-
*
448-
* @param {CanvasRenderingContext2D} context the target canvas context
449-
* @param {string} text the token's text content
450-
* @param {string} color the token's CSS color
451-
* @param {number} x the x position of the token in the line
452-
* @param {number} y the y position of the line in the minimap
453-
* @param {number} charWidth the width of a character in the minimap
454-
* @param {number} charHeight the height of a character in the minimap
455-
* @return {number} the x position at the end of the token
456-
* @access private
457-
*/
458-
drawToken (context, text, color, x, y, charWidth, charHeight) {
459-
context.fillStyle = color
460-
461-
if (this.ignoreWhitespacesInTokens) {
462-
const length = text.length * charWidth
463-
context.fillRect(x, y, length, charHeight)
464-
465-
return x + length
466-
} else {
467-
let chars = 0
468-
for (let j = 0, len = text.length; j < len; j++) {
469-
const char = text[j]
470-
if (/\s/.test(char)) {
471-
if (chars > 0) {
472-
context.fillRect(x - (chars * charWidth), y, chars * charWidth, charHeight)
473-
}
474-
chars = 0
475-
} else {
476-
chars++
477-
}
478-
x += charWidth
479-
}
480-
if (chars > 0) {
481-
context.fillRect(x - (chars * charWidth), y, chars * charWidth, charHeight)
482-
}
483-
return x
484-
}
485-
}
486-
487445
/**
488446
* Draws the specified decorations for the current `screenRow`.
489447
*
@@ -684,6 +642,49 @@ export default class CanvasDrawer extends Mixin {
684642
// ## ## ## ## ## ## ## ## ##
685643
// ######## ## ## ## ## ### ###
686644

645+
/**
646+
* Draws a single token on the given context.
647+
*
648+
* @param {CanvasRenderingContext2D} context the target canvas context
649+
* @param {string} text the token's text content
650+
* @param {string} color the token's CSS color
651+
* @param {number} x the x position of the token in the line
652+
* @param {number} y the y position of the line in the minimap
653+
* @param {number} charWidth the width of a character in the minimap
654+
* @param {number} charHeight the height of a character in the minimap
655+
* @return {number} the x position at the end of the token
656+
* @return {boolean} the x position at the end of the token
657+
* @access private
658+
*/
659+
function drawToken (context, text, color, x, y, charWidth, charHeight, ignoreWhitespacesInTokens) {
660+
context.fillStyle = color
661+
662+
if (ignoreWhitespacesInTokens) {
663+
const length = text.length * charWidth
664+
context.fillRect(x, y, length, charHeight)
665+
666+
return x + length
667+
} else {
668+
let chars = 0
669+
for (let j = 0, len = text.length; j < len; j++) {
670+
const char = text[j]
671+
if (/\s/.test(char)) {
672+
if (chars > 0) {
673+
context.fillRect(x - (chars * charWidth), y, chars * charWidth, charHeight)
674+
}
675+
chars = 0
676+
} else {
677+
chars++
678+
}
679+
x += charWidth
680+
}
681+
if (chars > 0) {
682+
context.fillRect(x - (chars * charWidth), y, chars * charWidth, charHeight)
683+
}
684+
return x
685+
}
686+
}
687+
687688
/**
688689
* Returns an array of tokens by line.
689690
*

0 commit comments

Comments
 (0)