All files / src/transformers gridTransformer.ts

95.68% Statements 133/139
92.4% Branches 73/79
100% Functions 12/12
95.68% Lines 133/139

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 2896x     6x           465x 465x 465x     465x               488x   488x 99x 99x   389x 389x     488x   488x 174x 174x   174x 174x 174x 174x 174x   174x 174x 174x 174x 174x   174x 99x     174x 174x     488x 175x 175x 175x   175x 13x     175x 2x     175x   11x 11x 11x 11x 11x 11x     164x 164x 164x 164x 164x     175x     175x 175x           175x   175x   2x 1x   1x     2x 1x   1x     2x 1x   1x     2x 1x   1x     3x 2x   1x     2x 1x   1x     2x 1x   1x     2x 1x   1x       2x 1x   1x                 156x         174x 174x   174x 174x   174x 99x 99x 99x     75x 75x 75x                 174x 174x 174x   174x 174x     174x 174x     174x 20x   20x 20x   154x 154x     154x 154x     174x             260x 2x   258x         396x     396x         20x   2x   1x   1x   2x   1x   2x   2x   5x   4x             174x 5x   169x         174x 5x   169x         174x       174x      
import { YamlStore } from 'src/yamlStore';
import { AttributeTransformer, Properties } from '.';
 
export class GridTransformer implements AttributeTransformer {
	private maxWidth: number;
	private maxHeight: number;
	private isCenter: boolean;
 
	constructor() {
		this.maxWidth = YamlStore.getInstance().options.width;
		this.maxHeight = YamlStore.getInstance().options.height;
		this.isCenter = YamlStore.getInstance().options.center;
	}
 
	private gridAttributeRegex =
		/^(?:(-?\d*(?:px)?)(?:\s*|x)(-?\d*(?:px)?)|(center|top|bottom|left|right|topleft|topright|bottomleft|bottomright))$/m;
 
	transform(element: Properties) {
 
		let defaultDrop;
		let defaultUnit;
 
		const isAbsolute = element.getAttribute('absolute') == 'true';
 
		if (isAbsolute) {
			defaultDrop = '480px 700px';
			defaultUnit = 'px';
		} else {
			defaultDrop = '50 100';
			defaultUnit = '%';
		}
 
		const drop = element.getAttribute('drop');
 
		if (drop != undefined) {
			const drag = element.getAttribute('drag') ?? defaultDrop;
			const grid = this.read(drag, drop, isAbsolute);
 
			if (grid != undefined) {
				const left = this.leftOf(grid) + defaultUnit;
				const top = this.topOf(grid) + defaultUnit;
				const height = this.heightOf(grid) + defaultUnit;
				const width = this.widthOf(grid) + defaultUnit;
 
				element.addStyle('position', 'absolute');
				element.addStyle('left', left);
				element.addStyle('top', top);
				element.addStyle('height', height);
				element.addStyle('width', width);
 
				if (isAbsolute) {
					element.addStyle('min-height', height);
				}
			}
			element.deleteAttribute('drag');
			element.deleteAttribute('drop');
		}
 
		if (element.getAttribute('align') || drop) {
			const flow = element.getAttribute('flow');
			const [align, alignItems, justifyContent, stretch] = this.getAlignment(element.getAttribute('align'), flow);
			const justifyCtx = element.getAttribute('justify-content') ?? justifyContent;
 
			if (align) {
				element.addAttribute('align', align, false);
			}
 
			if (stretch) {
				element.addClass(stretch);
			}
 
			switch (flow) {
				case 'row':
					element.addStyle('display', 'flex');
					element.addStyle('flex-direction', 'row');
					element.addStyle('align-items', alignItems);
					element.addStyle('justify-content', justifyCtx);
					element.addClass('flex-even');
					break;
				case 'col':
				default:
					element.addStyle('display', 'flex');
					element.addStyle('flex-direction', 'column');
					element.addStyle('align-items', alignItems);
					element.addStyle('justify-content', justifyCtx);
					break;
			}
 
			Iif (this.isCenter != undefined && !this.isCenter) {
				element.addStyle('align-items', 'start');
			}
			element.deleteAttribute('flow');
			element.deleteAttribute('justify-content');
		}
 
	}
 
	getAlignment(input: string, flow: string): [string, string, string, string] {
		const direction = flow ?? 'col';
 
		switch (input) {
			case 'topleft':
				if (direction == 'col') {
					return ['left', 'flex-start', 'flex-start', undefined];
				} else {
					return ['left', 'flex-start', 'space-evenly', undefined];
				}
			case 'topright':
				if (direction == 'col') {
					return ['right', 'flex-end', 'flex-start', undefined];
				} else {
					return ['right', 'flex-start', 'space-evenly', undefined];
				}
			case 'bottomright':
				if (direction == 'col') {
					return ['right', 'flex-end', 'flex-end', undefined];
				} else {
					return ['right', 'flex-end', 'space-evenly', undefined];
				}
			case 'bottomleft':
				if (direction == 'col') {
					return ['left', 'flex-start', 'flex-end', undefined];
				} else {
					return ['left', 'flex-end', 'space-evenly', undefined];
				}
			case 'left':
				if (direction == 'col') {
					return ['left', 'flex-start', 'space-evenly', undefined];
				} else {
					return ['left', 'center', 'space-evenly', undefined];
				}
			case 'right':
				if (direction == 'col') {
					return ['right', 'flex-end', 'space-evenly', undefined];
				} else {
					return ['right', 'center', 'space-evenly', undefined];
				}
			case 'top':
				if (direction == 'col') {
					return [undefined, 'center', 'flex-start', undefined];
				} else {
					return [undefined, 'flex-start', 'space-evenly', undefined];
				}
			case 'bottom':
				if (direction == 'col') {
					return [undefined, 'center', 'flex-end', undefined];
				} else {
					return [undefined, 'flex-end', 'space-evenly', undefined];
				}
 
			case 'stretch':
				if (direction == 'col') {
					return [undefined, 'center', 'space-evenly', 'stretch-column'];
				} else {
					return [undefined, 'center', 'space-evenly', 'stretch-row'];
				}
 
			case 'block':
			case 'justify':
				return ['justify', 'center', 'space-evenly', undefined];
			case 'center':
			default:
				// align - alignItems - justifyContent
				return [undefined, 'center', 'center', undefined];
		}
	}
 
	read(drag: string, drop: string, isAbsolute: boolean): Map<string, number> {
		try {
			const result = new Map<string, number>();
 
			result.set('slideWidth', this.maxWidth);
			result.set('slideHeight', this.maxHeight);
 
			if (isAbsolute) {
				result.set('maxWidth', this.maxWidth);
				result.set('maxHeight', this.maxHeight);
				return this.readValues(drag, drop, result, this.toPixel);
 
			} else {
				result.set('maxWidth', 100);
				result.set('maxHeight', 100);
				return this.readValues(drag, drop, result, this.toRelativeValue);
			}
		} catch (ex) {
			return undefined;
		}
	}
 
	readValues(drag: string, drop: string, result: Map<string, number>, valueTransformer: (max: number, input: string) => number): Map<string, number> {
 
		try {
			const [, width, height] = this.gridAttributeRegex.exec(drag);
			const [, x, y, name] = this.gridAttributeRegex.exec(drop);
 
			if (width) {
				result.set('width', valueTransformer(result.get('slideWidth'), width));
			}
 
			if (height) {
				result.set('height', valueTransformer(result.get('slideHeight'), height));
			}
 
			if (name) {
				const [nx, ny] = this.getXYof(name, result.get('width'), result.get('height'), result.get('maxWidth'), result.get('maxHeight'));
 
				result.set('x', nx);
				result.set('y', ny);
			} else {
				if (x) {
					result.set('x', valueTransformer(result.get('slideWidth'), x));
				}
 
				if (y) {
					result.set('y', valueTransformer(result.get('slideHeight'), y));
				}
			}
			return result;
		} catch (ex) {
			return undefined;
		}
	}
 
	toRelativeValue(max: number, input: string): number {
		if (input.toLowerCase().endsWith('px')) {
			return Number(input.toLowerCase().replace('px', '').trim()) / max * 100;
		} else {
			return Number(input);
		}
	}
 
	toPixel(max: number, input: string): number {
		Iif (input.toLowerCase().endsWith('px')) {
			return Number(input.toLowerCase().replace('px', '').trim());
		} else {
			return (max / 100) * Number(input);
		}
	}
 
	getXYof(name: string, width: number, height: number, maxWidth: number, maxHeight: number): [number, number] {
		switch (name) {
			case 'topleft':
				return [0, 0];
			case 'topright':
				return [maxWidth - width, 0];
			case 'bottomleft':
				return [0, maxHeight - height];
			case 'bottomright':
				return [maxWidth - width, maxHeight - height];
			case 'left':
				return [0, (maxHeight - height) / 2];
			case 'right':
				return [maxWidth - width, (maxHeight - height) / 2];
			case 'top':
				return [(maxWidth - width) / 2, 0];
			case 'bottom':
				return [(maxWidth - width) / 2, maxHeight - height];
			case 'center':
				return [(maxWidth - width) / 2, (maxHeight - height) / 2];
			default:
				return [0, 0];
		}
	}
 
	leftOf(grid: Map<string, number>): number {
		if (grid.get('x') < 0) {
			return grid.get('maxWidth') + grid.get('x') - grid.get('width');
		} else {
			return grid.get('x');
		}
	}
 
	topOf(grid: Map<string, number>): number {
		if (grid.get('y') < 0) {
			return grid.get('maxHeight') + grid.get('y') - grid.get('height');
		} else {
			return grid.get('y');
		}
	}
 
	heightOf(grid: Map<string, number>): number {
		return grid.get('height');
	}
 
	widthOf(grid: Map<string, number>): number {
		return grid.get('width');
	}
}