All files / client toolbar.js

88.88% Statements 64/72
75.86% Branches 22/29
81.25% Functions 13/16
88.88% Lines 64/72

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    2x     2x   2x 52x 902x 875x 875x 859x           2x 57x 52x         50x   52x 52x 52x   57x 56x       2x 54x             54x 52x 52x     54x 24x 24x     54x 54x     2x 6x           6x 7x     7x 6x 6x   4x 4x 4x 4x         2x 2x     5x     6x   6x         6x 6x     2x 4x 3x   4x     2x 4x 4x     4x 4x 4x       4x     2x 5x 5x     2x 72x 72x         2x             2x                    
import State from './state.js';
 
const Toolbar = (() => {
	let currentButton;
	let previousButton;
	const tools = {};
 
	const blur = () => {
		Object.values(tools).forEach(tool => {
			if (tool.isLoaded) {
				tool.button.classList.remove('toolbarDisplayed');
				if (typeof tool.onBlur === 'function') {
					tool.onBlur();
				}
			}
		});
	};
 
	const _activateTool = (tool, onFocus) => {
		if (currentButton !== tool.button) {
			if (
				currentButton !== undefined &&
				currentButton.id !== 'shapes' &&
				currentButton.id !== 'brushes'
			) {
				previousButton = currentButton;
			}
			blur(); // Deactivate the current tool
			tool.button.classList.add('toolbarDisplayed');
			currentButton = tool.button;
		}
		if (typeof onFocus === 'function') {
			onFocus();
		}
	};
 
	const add = (button, onFocus, onBlur) => {
		const tool = {
			button: button,
			onFocus: onFocus,
			onBlur: onBlur,
			isLoaded: true,
		};
 
		tool.enable = () => {
			closeMenu();
			_activateTool(tool, tool.onFocus);
		};
 
		button.addEventListener('click', e => {
			e.preventDefault();
			tool.enable();
		});
 
		tools[button.id] = tool;
		return { enable: tool.enable };
	};
 
	const addLazy = (button, toolLoader) => {
		const tool = {
			button: button,
			toolLoader: toolLoader,
			isLoaded: false,
		};
 
		const enable = async () => {
			closeMenu();
 
			// If the tool is not loaded, load it first.
			if (!tool.isLoaded) {
				try {
					const loadedTool = await tool.toolLoader();
					// Once loaded, update the tool's definition with its actual implementation.
					tool.onFocus = loadedTool.onFocus;
					tool.onBlur = loadedTool.onBlur;
					tool.isLoaded = true;
					tool.enable = () => {
						closeMenu();
						_activateTool(tool, tool.onFocus);
					};
				} catch (error) {
					console.error(`Failed to load tool for ${button.id}:`, error);
					return;
				}
			}
			_activateTool(tool, tool.onFocus);
		};
 
		tool.enable = enable;
 
		button.addEventListener('click', e => {
			e.preventDefault();
			tool.enable();
		});
 
		tools[button.id] = tool;
		return { enable: tool.enable };
	};
 
	const switchTool = toolId => {
		if (tools[toolId]) {
			tools[toolId].enable();
		}
		closeMenu();
	};
 
	const returnToPreviousTool = () => {
		Eif (previousButton) {
			Iif (State.selectionTool.isMoveMode()) {
				State.selectionTool.toggleMoveMode();
			} else {
				const toolId = previousButton.id;
				Eif (tools[toolId]) {
					tools[toolId].enable();
				}
			}
		}
		closeMenu();
	};
 
	const getCurrentTool = () => {
		closeMenu();
		return currentButton ? currentButton.id : null;
	};
 
	const closeMenu = () => {
		Eif (State.menus) {
			State.menus.close();
		}
	};
 
	// Escape key - return to previous tool
	document.addEventListener('keydown', e => {
		if (e.code === 'Escape') {
			e.preventDefault();
			returnToPreviousTool();
		}
	});
 
	return {
		add: add,
		addLazy: addLazy,
		switchTool: switchTool,
		returnToPreviousTool: returnToPreviousTool,
		getCurrentTool: getCurrentTool,
	};
})();
 
export default Toolbar;