UNPKG

@react-slate/core

Version:

Write interactive CLI apps with React

87 lines 3.1 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const Text_1 = __importDefault(require("../nodes/Text")); class Canvas { constructor() { this.pixels = []; this.width = 0; this.height = 0; this.x = 0; this.y = 0; } resize(layout) { for (let y = 0; y < layout.height; y += 1) { this.pixels[y] = []; for (let x = 0; x < layout.width; x += 1) { this.pixels[y][x] = { char: '', z: -Number.MAX_VALUE, }; } } this.width = layout.width; this.height = layout.height; this.x = layout.x; this.y = layout.y; } fillPixel({ x, y, z }, char, style) { // Pixel is below already stored pixel if (z < this.pixels[y][x].z) { return; } if (char) { this.pixels[y][x].char = char; } if (this.pixels[y][x].z === z) { if (style) { this.pixels[y][x].style = { color: style.color || (this.pixels[y][x].style && this.pixels[y][x].style.color), bgColor: style.bgColor || (this.pixels[y][x].style && this.pixels[y][x].style.bgColor), modifiers: style.modifiers || (this.pixels[y][x].style && this.pixels[y][x].style.modifiers), }; } } else { this.pixels[y][x].style = { color: style ? style.color : undefined, bgColor: style ? style.bgColor : undefined, modifiers: style ? style.modifiers : undefined, }; } this.pixels[y][x].z = z; } fill(node, layout, { parentZ }) { this.resize(layout); const body = node instanceof Text_1.default && node.getBody(); for (let y = 0; y < layout.height; y++) { // Body X index let bx = 0; for (let x = 0; x < layout.width; x += 1) { this.fillPixel({ x, y, z: node.isAbsolute ? node.zIndex : parentZ }, body ? body[bx] : undefined, node.style); bx += 1; } } } mergeChildCanvas(childCanvas) { for (let cy = childCanvas.y, y = 0; cy < childCanvas.y + childCanvas.height && cy < this.height; cy += 1, y += 1) { if (cy < 0) { continue; } for (let cx = childCanvas.x, x = 0; cx < childCanvas.x + childCanvas.width && cx < this.width; cx += 1, x += 1) { if (cx < 0) { continue; } const pixel = childCanvas.pixels[y][x]; this.fillPixel({ x: cx, y: cy, z: pixel.z }, pixel.char, pixel.style); } } } } exports.default = Canvas; //# sourceMappingURL=Canvas.js.map