Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 | 15x 15x 15x 15x 229x 15x 586x 7x 579x 15x 24680x 24680x 24680x 24680x 15x 22x 22x 2x 2x 2x 20x 20x 22x 7x 7x 13x 13x 13x 15x 52x 52x 52x 52x 52x 52x 52x 52x 4x 4x 52x 450x 52x 51x 51x 51x 50x 6x 6x 6x 50x 50x 50x 50x 50x 50x 1x 1x 1x 1x 52x 24x 24x 216x 193x 24x 52x 24x 22x 22x 24x 7x 7x 24x 23x 23x 11x 11x 11x 11x 11x 52x 52x 3x 3x 52x 7x 52x 52x 15x 10x 10x 10x 14x 14x 6x 8x 10x 2x 2x 2x 10x 2x 2x 2x 2x 10x 10x 10x 15x 4x 4x 2x 2x 2x 15x 159x 16x 13x 16x 159x 159x 15x 4x 4x 2x 15x 24x 18x 24x 15x 6x 6x 6x 6x 4x 4x 4x 4x 6x 4x 4x 4x 4x 2x 4x 6x 2x 15x 10x 4x 4x 6x 4x 4x 15x 8x 8x 6x 8x 18x 14x 14x 4x 4x 4x 4x 10x 6x 6x 6x 8x 18x 14x 8x 8x 8x 8x 8x 4x 8x 2x 8x 8x 15x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 24x 24x 35x 19x 19x 35x 14x 14x 35x 11x 11x 35x 15x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 50x 50x 32000x 2x 160x 160x 64000x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 15x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 15x 7364x 15x 4x 4x 4x 4x 4x 7360x 7360x 4x 2x 4x 15x 6x 2x 2x 6x 2x 2x 6x 15x 15x 2x 8x 2x 2x 2x 2x 2x 15x 4x 4x 6x 6x 6x 4x 4x 4x 4x 4x 4x 4x 2x 2x 2x 2x 2x 2x 15x 19x 19x 19x 18x 34x 18x 19x 2x 2x 19x 13x 13x 13x 12x 12x 1x 19x 36x 36x 36x 36x 13x 36x 52x 36x 19x 19x 15x 35x 35x 35x 268x 118x 169x 118x 6x 13x 6x 3x 3x 3x 65x 65x 234x 234x 234x 65x 65x 65x 65x 109x 109x 109x 109x 109x 35x 9x 9x 4x 4x 5x 1x 1x 4x 1x 1x 3x 1x 1x 2x 2x 2x 35x 118x 9x 9x 35x 35x 35x 35x 35x 35x 35x 35x 15x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 5x 5x 5x 5x 5x 5x 5x 15x 10x 20x 24x | import State from './state.js';
import { loadFontFromXBData } from './font.js';
// Utilities for DOM manipulation
const D = document,
$ = D.getElementById.bind(D),
$$ = D.querySelector.bind(D),
$$$ = D.querySelectorAll.bind(D),
has = (i, c) => !!i && i.classList.contains(c),
classList = (el, className, add = true) => {
if (!el || !el.classList) {
return;
}
add ? el.classList.add(className) : el.classList.remove(className);
};
const createCanvas = (width, height) => {
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
return canvas;
};
const toggleFullscreen = () => {
// Check if any fullscreen API is available
const isFullscreenEnabled =
document.fullscreenEnabled || document.webkitFullscreenEnabled;
if (!isFullscreenEnabled) {
console.warn('[UI] Fullscreen not supported');
$('fullscreen').classList.add('disabled');
return;
}
try {
const fullscreenElement =
document.fullscreenElement ||
document.webkitFullscreenElement ||
document.webkitCurrentFullScreenElement; // Safari specific
if (fullscreenElement) {
if (document.exitFullscreen) {
document.exitFullscreen();
} else Eif (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
} else if (document.webkitCancelFullScreen) {
document.webkitCancelFullScreen(); // Older Safari
}
} else {
const element = document.documentElement;
if (element.requestFullscreen) {
element.requestFullscreen();
} else Eif (element.webkitRequestFullscreen) {
element.webkitRequestFullscreen();
} else if (element.webkitRequestFullScreen) {
element.webkitRequestFullScreen(); // Older Safari (capital S)
}
}
} catch {
console.error('[UI] Failed toggling fullscreen mode');
}
};
// Modal
const createModalController = modal => {
const modals = [
$('aboutModal'),
$('resizeModal'),
$('fontsModal'),
$('sauceModal'),
$('choiceModal'),
$('updateModal'),
$('loadingModal'),
$('warningModal'),
$('tutorialsModal'),
];
let current = false;
let closingTimeout = null;
let backdropHandler = null;
let cleanupHandler = null;
let focus = () => {};
let blur = () => {};
const focusEvents = (onFocus, onBlur) => {
focus = onFocus;
blur = onBlur;
};
const currentScreen = () => current;
const clear = () => modals.forEach(s => classList(s, 'hide'));
const open = name => {
current = name;
const section = name + 'Modal';
if ($(section)) {
// cancel current close event
if (closingTimeout) {
clearTimeout(closingTimeout);
closingTimeout = null;
classList(modal, 'closing', false);
}
clear();
focus();
classList($(section), 'hide', false);
modal.showModal();
Eif (name !== 'update') {
backdropHandler = onClick(modal, e => {
Eif (e.target === modal) {
close();
}
});
}
} else {
error(`Unknown modal: <kbd>#{section}</kbd>`);
console.error(`Unknown modal: <kbd>#{section}</kbd>`);
}
};
const queued = () => {
let i = 0;
modals.forEach(s => {
if (has(s, 'hide')) {
i++;
}
});
return i !== modals.length - 1 ? true : false;
};
const close = () => {
if (typeof backdropHandler === 'function') {
backdropHandler();
backdropHandler = null;
}
if (typeof cleanupHandler === 'function') {
cleanupHandler();
cleanupHandler = null;
}
if (!queued()) {
classList(modal, 'closing');
closingTimeout = setTimeout(() => {
blur();
classList(modal, 'closing', false);
modal.close();
current = false;
closingTimeout = null;
}, 700);
}
};
const loading = (message = 'Reinitializing editor state...') => {
if (current !== 'loading') {
$('loadingMsg').innerHTML = message;
}
open('loading');
};
const error = message => {
$('modalError').innerHTML = message;
open('error');
};
const onClose = handler => {
cleanupHandler = handler;
};
// attach to all close buttons
$$$('.close').forEach(b => onClick(b, _ => close()));
return {
isOpen: () => modal.open,
current: currentScreen,
open: open,
close: close,
error: error,
focusEvents: focusEvents,
loading: loading,
onClose: onClose,
};
};
// Toggles
const createSettingToggle = (el, getter, setter) => {
let currentSetting;
let g = getter;
let s = setter;
const update = () => {
currentSetting = g();
if (currentSetting) {
el.classList.add('enabled');
} else {
el.classList.remove('enabled');
}
};
const sync = (getter, setter) => {
g = getter;
s = setter;
update();
};
const changeSetting = e => {
e.preventDefault();
currentSetting = !currentSetting;
s(currentSetting);
update();
};
el.addEventListener('click', changeSetting);
update();
return {
sync: sync,
update: update,
};
};
const onReturn = (el, target) => {
el.addEventListener('keypress', e => {
if (!e.altKey && !e.ctrlKey && !e.metaKey && e.code === 'Enter') {
// Enter key
e.preventDefault();
e.stopPropagation();
target.click();
}
});
};
const onClick = (el, func) => {
const handler = e => {
if (el.tagName === 'A' || el.tagName === 'BUTTON') {
e.preventDefault();
}
func(e, el);
};
el.addEventListener('click', handler);
return () => el.removeEventListener('click', handler);
};
const onFileChange = (el, func) => {
el.addEventListener('change', e => {
if (e.target.files.length > 0) {
func(e.target.files[0]);
}
});
};
const createPositionInfo = el => {
const update = (x, y) => {
el.textContent = x + 1 + ', ' + (y + 1);
};
return { update: update };
};
const viewportTap = view => {
const maxTapDuration = 300;
let touchStartTime = 0;
let activeTouches = 0;
view.addEventListener('touchstart', event => {
activeTouches = event.touches.length;
Eif (activeTouches === 2) {
touchStartTime = Date.now();
}
State.menus.close();
});
view.addEventListener('touchend', event => {
Eif (activeTouches === 2 && event.changedTouches.length === 2) {
const endTime = Date.now();
const tapDuration = endTime - touchStartTime;
if (tapDuration < maxTapDuration) {
State.textArtCanvas.undo();
}
}
activeTouches = 0;
});
view.addEventListener('touchcancel', _ => {
activeTouches = 0;
});
};
const undoAndRedo = e => {
if ((e.ctrlKey || (e.metaKey && !e.shiftKey)) && e.code === 'KeyZ') {
// Ctrl/Cmd+Z - Undo
e.preventDefault();
State.textArtCanvas.undo();
} else if (
(e.ctrlKey && e.code === 'KeyY') ||
(e.metaKey && e.shiftKey && e.code === 'KeyZ')
) {
// Ctrl+Y or Cmd+Shift+Z - Redo
e.preventDefault();
State.textArtCanvas.redo();
}
};
const createPaintShortcuts = keyPair => {
let ignored = false;
const isConnected = e =>
!State.network ||
!State.network.isConnected() ||
!keyPair[e.key].classList.contains('excludedForWebsocket');
const keyDown = e => {
if (!ignored) {
Eif (!e.ctrlKey && !e.altKey && !e.shiftKey && !e.metaKey) {
if (e.key >= '0' && e.key <= '7') {
// Number keys 0-7 for color shortcuts
const color = parseInt(e.key, 10);
const currentColor = State.palette.getForegroundColor();
Iif (currentColor === color) {
State.palette.setForegroundColor(color + 8);
} else {
State.palette.setForegroundColor(color);
}
} else {
// Use the actual key character for lookup
if (keyPair[e.key] !== undefined) {
Eif (isConnected(e)) {
e.preventDefault();
keyPair[e.key].click();
}
}
}
}
}
};
const keyDownWithCtrl = e => {
if (!ignored) {
Iif (e.ctrlKey && !e.altKey && !e.shiftKey && !e.metaKey) {
// Use the actual key character for lookup
if (keyPair[e.key] !== undefined) {
if (isConnected(e)) {
e.preventDefault();
keyPair[e.key].click();
}
}
}
}
};
document.addEventListener('keydown', keyDownWithCtrl);
const enable = () => {
document.addEventListener('keydown', keyDown);
};
const disable = () => {
document.removeEventListener('keydown', keyDown);
};
const ignore = () => {
ignored = true;
};
const unignore = () => {
ignored = false;
};
enable();
return {
enable: enable,
disable: disable,
ignore: ignore,
unignore: unignore,
};
};
const createToggleButton = (
stateOneName,
stateTwoName,
stateOneClick,
stateTwoClick,
) => {
const container = document.createElement('DIV');
container.classList.add('toggleButtonContainer');
const stateOne = document.createElement('DIV');
stateOne.classList.add('toggleButton');
stateOne.classList.add('left');
stateOne.textContent = stateOneName;
const stateTwo = document.createElement('DIV');
stateTwo.classList.add('toggleButton');
stateTwo.classList.add('right');
stateTwo.textContent = stateTwoName;
container.appendChild(stateOne);
container.appendChild(stateTwo);
const getElement = () => {
return container;
};
const setStateOne = () => {
stateOne.classList.add('enabled');
stateTwo.classList.remove('enabled');
};
const setStateTwo = () => {
stateTwo.classList.add('enabled');
stateOne.classList.remove('enabled');
};
stateOne.addEventListener('click', _ => {
setStateOne();
stateOneClick();
});
stateTwo.addEventListener('click', _ => {
setStateTwo();
stateTwoClick();
});
return {
getElement: getElement,
setStateOne: setStateOne,
setStateTwo: setStateTwo,
};
};
const createGrid = el => {
let canvases = [];
let enabled = false;
const createCanvases = () => {
const fontWidth = State.font.getWidth();
const fontHeight = State.font.getHeight();
const columns = State.textArtCanvas.getColumns();
const rows = State.textArtCanvas.getRows();
const canvasWidth = fontWidth * columns;
const canvasHeight = fontHeight * 25;
canvases = [];
for (let i = 0; i < Math.floor(rows / 25); i++) {
const canvas = createCanvas(canvasWidth, canvasHeight);
canvases.push(canvas);
}
Iif (rows % 25 !== 0) {
const canvas = createCanvas(canvasWidth, fontHeight * (rows % 25));
canvases.push(canvas);
}
};
const renderGrid = canvas => {
const columns = State.textArtCanvas.getColumns();
const rows = Math.min(State.textArtCanvas.getRows(), 25);
const fontWidth = canvas.width / columns;
const fontHeight = State.font.getHeight();
const ctx = canvas.getContext('2d');
const imageData = ctx.createImageData(canvas.width, canvas.height);
const byteWidth = canvas.width * 4;
const darkGray = new Uint8Array([63, 63, 63, 255]);
for (let y = 0; y < rows; y += 1) {
for (
let x = 0, i = y * fontHeight * byteWidth;
x < canvas.width;
x += 1, i += 4
) {
imageData.data.set(darkGray, i);
}
}
for (let x = 0; x < columns; x += 1) {
for (
let y = 0, i = x * fontWidth * 4;
y < canvas.height;
y += 1, i += byteWidth
) {
imageData.data.set(darkGray, i);
}
}
ctx.putImageData(imageData, 0, 0);
};
const createGrid = () => {
createCanvases();
renderGrid(canvases[0]);
el.appendChild(canvases[0]);
for (let i = 1; i < canvases.length; i++) {
canvases[i].getContext('2d').drawImage(canvases[0], 0, 0);
el.appendChild(canvases[i]);
}
};
const resize = () => {
canvases.forEach(canvas => {
el.removeChild(canvas);
});
createGrid();
};
createGrid();
document.addEventListener('onTextCanvasSizeChange', resize);
document.addEventListener('onLetterSpacingChange', resize);
document.addEventListener('onFontChange', resize);
document.addEventListener('onScaleFactorChange', resize);
document.addEventListener('onOpenedFile', resize);
const isShown = () => {
return enabled;
};
const show = turnOn => {
if (enabled && !turnOn) {
el.classList.remove('enabled');
enabled = false;
} else if (!enabled && turnOn) {
el.classList.add('enabled');
enabled = true;
}
};
return {
isShown: isShown,
show: show,
};
};
const createToolPreview = el => {
let canvases = [];
let ctxs = [];
const createCanvases = () => {
const fontWidth = State.font.getWidth();
const fontHeight = State.font.getHeight();
const columns = State.textArtCanvas.getColumns();
const rows = State.textArtCanvas.getRows();
const canvasWidth = fontWidth * columns;
const canvasHeight = fontHeight * 25;
canvases = new Array();
ctxs = new Array();
for (let i = 0; i < Math.floor(rows / 25); i++) {
const canvas = createCanvas(canvasWidth, canvasHeight);
canvases.push(canvas);
ctxs.push(canvas.getContext('2d'));
}
Iif (rows % 25 !== 0) {
const canvas = createCanvas(canvasWidth, fontHeight * (rows % 25));
canvases.push(canvas);
ctxs.push(canvas.getContext('2d'));
}
canvases.forEach(canvas => {
el.appendChild(canvas);
});
};
const resize = () => {
canvases.forEach(canvas => {
el.removeChild(canvas);
});
createCanvases();
};
const drawHalfBlock = (foreground, x, y) => {
const halfBlockY = y % 2;
const textY = Math.floor(y / 2);
const ctxIndex = Math.floor(textY / 25);
if (ctxIndex >= 0 && ctxIndex < ctxs.length) {
State.font.drawWithAlpha(
halfBlockY === 0 ? 223 : 220,
foreground,
ctxs[ctxIndex],
x,
textY % 25,
);
}
};
const clear = () => {
for (let i = 0; i < ctxs.length; i++) {
ctxs[i].clearRect(0, 0, canvases[i].width, canvases[i].height);
}
};
createCanvases();
el.classList.add('enabled');
document.addEventListener('onTextCanvasSizeChange', resize);
document.addEventListener('onLetterSpacingChange', resize);
document.addEventListener('onFontChange', resize);
document.addEventListener('onOpenedFile', resize);
return {
clear: clear,
drawHalfBlock: drawHalfBlock,
};
};
const getUtf8Bytes = str => {
return new TextEncoder().encode(str).length;
};
const enforceMaxBytes = () => {
const SAUCE_MAX_BYTES = 16320;
const sauceComments = $('sauceComments');
let val = sauceComments.value;
let bytes = getUtf8Bytes(val);
while (bytes > SAUCE_MAX_BYTES) {
val = val.slice(0, -1);
bytes = getUtf8Bytes(val);
}
if (val !== sauceComments.value) {
sauceComments.value = val;
}
$('sauceBytes').value = `${bytes}/${SAUCE_MAX_BYTES} bytes`;
};
const createGenericController = (panel, nav) => {
const enable = () => {
panel.style.display = 'flex';
nav.classList.add('enabledParent');
};
const disable = () => {
panel.style.display = 'none';
nav.classList.remove('enabledParent');
};
return {
enable: enable,
disable: disable,
};
};
const createViewportController = el => {
const panel = el;
const enable = () => {
panel.style.display = 'flex';
};
const disable = () => {
panel.style.display = 'none';
};
return {
enable: enable,
disable: disable,
};
};
const createResolutionController = (lbl, txtC, txtR) => {
[
'onTextCanvasSizeChange',
'onFontChange',
'onXBFontLoaded',
'onOpenedFile',
].forEach(e => {
document.addEventListener(e, _ => {
const cols = State.textArtCanvas.getColumns();
const rows = State.textArtCanvas.getRows();
lbl.innerText = `${cols}x${rows}`;
txtC.value = cols;
txtR.value = rows;
});
});
};
const createDragDropController = (handler, el) => {
let dragCounter = 0;
document.addEventListener('dragenter', e => {
e.preventDefault();
dragCounter++;
el.style.display = 'flex';
});
document.addEventListener('dragover', e => {
e.preventDefault();
});
document.addEventListener('dragleave', e => {
e.preventDefault();
dragCounter--;
Eif (dragCounter === 0) {
el.style.display = 'none';
}
});
document.addEventListener('drop', e => {
e.preventDefault();
dragCounter = 0;
el.style.display = 'none';
const files = e.dataTransfer?.files;
Eif (files && files.length > 0) {
handler(files[0]);
}
});
};
const createMenuController = (elements, canvas, view) => {
const menus = [];
let current = null;
const closeAll = () => {
menus.forEach(menu => {
classList(menu, 'hide', true);
});
canvas.focus();
};
const close = _ => {
current = null;
closeAll();
};
const toggle = menu => {
current = has(menu, 'hide') ? menu : null;
closeAll();
if (current) {
classList(current, 'hide', false);
menu.focus();
} else {
canvas.focus();
}
};
elements.forEach(el => {
const button = el.button;
const menu = el.menu;
menus.push(menu);
onClick(button, _ => {
toggle(menu);
});
menu.querySelectorAll('.menuItem').forEach(item => {
onClick(item, close);
});
menu.addEventListener('blur', close);
});
onClick(view, close);
return { close: closeAll };
};
const createFontSelect = (el, lbl, img, btn) => {
const listbox = el;
const previewInfo = lbl;
const previewImage = img;
function getOptions() {
return Array.from(listbox.querySelectorAll('[role="option"]'));
}
function getValue() {
const selected = getOptions().find(
opt => opt.getAttribute('aria-selected') === 'true',
);
return selected ? selected.dataset.value || selected.textContent : null;
}
function setValue(value) {
const options = getOptions();
const idx = options.findIndex(opt => opt.dataset.value === value);
if (idx === -1) {
return false;
}
updateSelection(idx);
return true;
}
function setFocus() {
const w8 = setTimeout(() => {
listbox.focus();
const options = getOptions();
const idx = options.findIndex(opt => opt.dataset.value === getValue());
options[idx].scrollIntoView({ block: 'nearest' });
clearTimeout(w8);
}, 100);
}
function updateSelection(idx) {
const options = getOptions();
options.forEach((opt, i) => {
const isSelected = i === idx;
opt.setAttribute('aria-selected', isSelected ? 'true' : 'false');
opt.classList.toggle('focused', isSelected);
});
listbox.setAttribute('aria-activedescendant', options[idx].id);
options[idx].scrollIntoView({ block: 'nearest' });
focusedIdx = idx;
updateFontPreview(getValue());
}
async function updateFontPreview(fontName) {
Iif (fontName === 'XBIN') {
const xbFontData = State.textArtCanvas.getXBFontData();
if (xbFontData && xbFontData.bytes) {
const xbfont = await loadFontFromXBData(
xbFontData.bytes,
xbFontData.width,
xbFontData.height,
xbFontData.letterSpacing,
State.palette,
);
const previewCanvas = createCanvas(
xbFontData.width * 16,
xbFontData.height * 16,
);
const previewCtx = previewCanvas.getContext('2d');
const foreground = 15,
background = 0;
for (let y = 0, charCode = 0; y < 16; y++) {
for (let x = 0; x < 16; x++, charCode++) {
xbfont.draw(charCode, foreground, background, previewCtx, x, y);
}
}
previewInfo.textContent =
'XBIN: embedded ' + xbFontData.width + 'x' + xbFontData.height;
previewImage.src = previewCanvas.toDataURL();
} else {
previewInfo.textContent = 'XBIN: none';
previewImage.src = `${State.fontDir}missing.png`;
}
} else {
const image = new Image();
image.onload = () => {
previewInfo.textContent = fontName;
previewImage.src = image.src;
};
image.onerror = () => {
previewInfo.textContent = fontName + ' (not found)';
image.src = `${State.fontDir}missing.png`;
};
image.src = `${State.fontDir}${fontName}.png`;
}
}
// Listeners
listbox.addEventListener('keydown', e => {
const options = getOptions();
if (e.key === 'ArrowDown' && focusedIdx < options.length - 1) {
e.preventDefault();
updateSelection(++focusedIdx);
} else if (e.key === 'ArrowUp' && focusedIdx > 0) {
e.preventDefault();
updateSelection(--focusedIdx);
} else if (e.key === 'Home') {
e.preventDefault();
updateSelection((focusedIdx = 0));
} else if (e.key === 'End') {
e.preventDefault();
updateSelection((focusedIdx = options.length - 1));
} else Eif (e.key === ' ' || e.key === 'Enter') {
e.preventDefault();
btn.click();
}
});
getOptions().forEach((opt, i) => {
opt.addEventListener('click', () => {
updateSelection(i);
updateFontPreview(getValue());
});
});
listbox.addEventListener('focus', () => updateSelection(focusedIdx));
listbox.addEventListener('blur', () =>
getOptions().forEach(opt => opt.classList.remove('focused')));
// Init
let focusedIdx = getOptions().findIndex(
opt => opt.getAttribute('aria-selected') === 'true',
);
Iif (focusedIdx < 0) {
focusedIdx = 0;
}
updateSelection(focusedIdx);
updateFontPreview(getValue());
return {
focus: setFocus,
getValue: getValue,
setValue: setValue,
};
};
const createZoomControl = () => {
const container = document.createElement('div');
container.className = 'zoomControl';
container.setAttribute('aria-label', 'Canvas Zoom Control');
const label = document.createElement('label');
label.textContent = 'Zoom';
label.htmlFor = 'zoomSlider';
const slider = document.createElement('input');
slider.type = 'range';
slider.id = 'zoomSlider';
slider.min = '0.5';
slider.max = '4';
slider.step = '0.5';
slider.value = '1';
slider.setAttribute('aria-valuemin', '0.5');
slider.setAttribute('aria-valuemax', '4');
slider.setAttribute('aria-valuenow', '1');
slider.setAttribute('aria-label', 'Canvas zoom level');
const display = document.createElement('span');
display.className = 'zoomDisplay';
display.textContent = '1.0x';
display.setAttribute('aria-live', 'polite');
const updateZoom = value => {
const scale = parseFloat(value);
display.textContent = `${scale.toFixed(1)}x`;
slider.setAttribute('aria-valuenow', value);
if (State.font && State.font.setScaleFactor) {
State.font.setScaleFactor(scale);
}
};
const updateSliderFromState = () => {
if (State.font && State.font.getScaleFactor) {
const currentScale = State.font.getScaleFactor();
slider.value = currentScale.toString();
display.textContent = `${currentScale.toFixed(1)}x`;
slider.setAttribute('aria-valuenow', currentScale.toString());
}
};
// Initialize from current font scale
State.waitFor('font', updateSliderFromState);
// Update slider UI when state is restored
document.addEventListener(
'onStateRestorationComplete',
updateSliderFromState,
);
// Event listeners
slider.addEventListener('input', e => {
// Update display during drag (preview)
const scale = parseFloat(e.target.value);
display.textContent = `${scale.toFixed(1)}x`;
});
slider.addEventListener('change', e => {
// Apply zoom on release
updateZoom(e.target.value);
// Save immediately to state (don't wait for debounced save)
if (State.saveToLocalStorage) {
State.saveToLocalStorage();
}
});
// Keyboard shortcuts
const handleKeyboardZoom = e => {
if ((e.ctrlKey || e.metaKey) && e.key === '=') {
// Ctrl/Cmd + Plus: Zoom in
e.preventDefault();
const currentValue = parseFloat(slider.value);
const newValue = Math.min(4, currentValue + 0.5);
slider.value = newValue.toString();
updateZoom(newValue.toString());
if (State.saveToLocalStorage) {
State.saveToLocalStorage();
}
} else if ((e.ctrlKey || e.metaKey) && e.key === '-') {
// Ctrl/Cmd + Minus: Zoom out
e.preventDefault();
const currentValue = parseFloat(slider.value);
const newValue = Math.max(0.5, currentValue - 0.5);
slider.value = newValue.toString();
updateZoom(newValue.toString());
if (State.saveToLocalStorage) {
State.saveToLocalStorage();
}
} else if ((e.ctrlKey || e.metaKey) && e.key === '0') {
// Ctrl/Cmd + 0: Reset to 1x
e.preventDefault();
slider.value = '1';
updateZoom('1');
if (State.saveToLocalStorage) {
State.saveToLocalStorage();
}
}
};
document.addEventListener('keydown', handleKeyboardZoom);
container.appendChild(label);
container.appendChild(display);
container.appendChild(slider);
return container;
};
const websocketUI = show => {
[
['excludedForWebsocket', !show],
['includedForWebsocket', show],
].forEach(([sel, prop]) =>
[...D.getElementsByClassName(sel)].forEach(
el => (el.style.display = prop ? 'block' : 'none'),
));
};
export {
$,
$$,
$$$,
createCanvas,
toggleFullscreen,
createModalController,
createSettingToggle,
onClick,
onReturn,
onFileChange,
createPositionInfo,
undoAndRedo,
viewportTap,
createViewportController,
createGenericController,
createPaintShortcuts,
createToggleButton,
createGrid,
createToolPreview,
enforceMaxBytes,
createResolutionController,
createDragDropController,
createMenuController,
createFontSelect,
createZoomControl,
websocketUI,
};
|