@node-projects/web-component-designer-zpl 0.2.1 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -2
- package/THIRD-PARTY-LICENSES.md +432 -0
- package/dist/assets/fonts/LICENSE-APACHE-2.0.txt +202 -0
- package/dist/assets/fonts/NOTICE.md +27 -0
- package/dist/assets/fonts/OCRA-NOTICE.md +27 -0
- package/dist/assets/fonts/OCRA.ttf +0 -0
- package/dist/assets/fonts/OCRB-NOTICE.md +21 -0
- package/dist/assets/fonts/OCRB.ttf +0 -0
- package/dist/assets/fonts/PrintLabMono-NOTICE.md +24 -0
- package/dist/assets/fonts/PrintLabMono.ttf +0 -0
- package/dist/assets/fonts/PrintLabZPL-Bold.ttf +0 -0
- package/dist/assets/fonts/PrintLabZPL-Bold.woff2 +0 -0
- package/dist/assets/fonts/VeraMono-Bold.ttf +0 -0
- package/dist/assets/fonts/VeraMono-NOTICE.md +57 -0
- package/dist/assets/fonts/VeraMono.ttf +0 -0
- package/dist/assets/licenses/BWIP-JS-LICENSE.txt +23 -0
- package/dist/assets/licenses/ZPLab-LICENSE.txt +21 -0
- package/dist/barcodes/barcodeRegistry.d.ts +124 -0
- package/dist/barcodes/barcodeRegistry.js +357 -0
- package/dist/barcodes/bwipRenderer.d.ts +90 -0
- package/dist/barcodes/bwipRenderer.js +630 -0
- package/dist/fonts/zplFonts.d.ts +27 -0
- package/dist/fonts/zplFonts.js +85 -0
- package/dist/index.d.ts +10 -3
- package/dist/index.js +10 -3
- package/dist/services/ZplBarcodeElementsService.d.ts +5 -0
- package/dist/services/ZplBarcodeElementsService.js +16 -0
- package/dist/services/ZplBarcodePropertiesService.d.ts +10 -0
- package/dist/services/ZplBarcodePropertiesService.js +59 -0
- package/dist/services/ZplDiagonalLineExtension.d.ts +46 -0
- package/dist/services/ZplDiagonalLineExtension.js +255 -0
- package/dist/services/ZplElementResizeStrategy.d.ts +20 -0
- package/dist/services/ZplElementResizeStrategy.js +182 -0
- package/dist/services/ZplGraphicResizeStrategy.d.ts +18 -0
- package/dist/services/ZplGraphicResizeStrategy.js +81 -0
- package/dist/services/ZplIncludeInOutputPropertiesService.d.ts +12 -0
- package/dist/services/ZplIncludeInOutputPropertiesService.js +50 -0
- package/dist/services/ZplParserService.d.ts +7 -4
- package/dist/services/ZplParserService.js +214 -249
- package/dist/services/ZplSelectionExtensionProvider.d.ts +9 -0
- package/dist/services/ZplSelectionExtensionProvider.js +15 -0
- package/dist/services/ZplTextEditExtension.d.ts +20 -0
- package/dist/services/ZplTextEditExtension.js +80 -0
- package/dist/services/ZplTextPropertiesService.d.ts +8 -0
- package/dist/services/ZplTextPropertiesService.js +24 -0
- package/dist/services/hiddenMetadata.d.ts +17 -0
- package/dist/services/hiddenMetadata.js +95 -0
- package/dist/services/zplResizeGeometry.d.ts +6 -0
- package/dist/services/zplResizeGeometry.js +8 -0
- package/dist/setupZplServiceContainer.js +22 -4
- package/dist/widgets/elements.json +0 -24
- package/dist/widgets/views/zpl-demo-view.js +27 -2
- package/dist/widgets/zpl-barcode.d.ts +68 -14
- package/dist/widgets/zpl-barcode.js +189 -74
- package/dist/widgets/zpl-comment.d.ts +2 -0
- package/dist/widgets/zpl-comment.js +6 -3
- package/dist/widgets/zpl-graphic-box.d.ts +12 -0
- package/dist/widgets/zpl-graphic-box.js +49 -15
- package/dist/widgets/zpl-graphic-circle.d.ts +2 -0
- package/dist/widgets/zpl-graphic-circle.js +7 -0
- package/dist/widgets/zpl-graphic-diagonal-line.d.ts +2 -0
- package/dist/widgets/zpl-graphic-diagonal-line.js +7 -0
- package/dist/widgets/zpl-image.d.ts +4 -1
- package/dist/widgets/zpl-image.js +17 -14
- package/dist/widgets/zpl-text.d.ts +38 -8
- package/dist/widgets/zpl-text.js +183 -49
- package/dist/zplHelper.d.ts +1 -1
- package/dist/zplHelper.js +5 -1
- package/package.json +9 -4
- package/dist/extensions/ZplLayoutResizeExtensionProvider.d.ts +0 -4
- package/dist/extensions/ZplLayoutResizeExtensionProvider.js +0 -16
- package/dist/jsBarcodeOptions.d.ts +0 -27
- package/dist/jsBarcodeOptions.js +0 -14
- package/dist/qr.d.ts +0 -11
- package/dist/qr.js +0 -759
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
const deviceFonts = {
|
|
2
|
+
A: { magStep: 9, magWidthStep: 5, advancePerMag: 6, capInkPerMag: 7.125, capPerEm: .757, advancePerEm: .602, yOffsetEm: .096, xOffsetEm: -.032, widthCorrection: .964, letterSpacingEm: .021 },
|
|
3
|
+
B: { magStep: 11, magWidthStep: 7, advancePerMag: 9, capInkPerMag: 11.5, capPerEm: .762, advancePerEm: .602, xOffsetEm: -.040 },
|
|
4
|
+
C: { magStep: 18, magWidthStep: 10, advancePerMag: 12, capInkPerMag: 14.75, capPerEm: .757, advancePerEm: .602, yOffsetEm: .026, xOffsetEm: -.043 },
|
|
5
|
+
D: { magStep: 18, magWidthStep: 10, advancePerMag: 12, capInkPerMag: 14.75, capPerEm: .757, advancePerEm: .602, yOffsetEm: .026, xOffsetEm: -.043 },
|
|
6
|
+
E: { magStep: 28, magWidthStep: 15, advancePerMag: 20, capInkPerMag: 21.875, capPerEm: .787, advancePerEm: .723, yOffsetEm: .198, xOffsetEm: -.189 },
|
|
7
|
+
F: { magStep: 26, magWidthStep: 13, advancePerMag: 16, capInkPerMag: 22, capPerEm: .757, advancePerEm: .602, yOffsetEm: .034, xOffsetEm: -.052 },
|
|
8
|
+
G: { magStep: 60, magWidthStep: 40, advancePerMag: 48, capInkPerMag: 48, capPerEm: .757, advancePerEm: .602, yOffsetEm: .055, widthCorrection: .817, letterSpacingEm: .142 },
|
|
9
|
+
H: { magStep: 21, magWidthStep: 13, advancePerMag: 19, capInkPerMag: 21, capPerEm: .780, advancePerEm: .723, yOffsetEm: -.030, xOffsetEm: -.193 }
|
|
10
|
+
};
|
|
11
|
+
export const zplFontFamilies = {
|
|
12
|
+
'0': 'ZplPrintLab', A: 'ZplPrintLabMono', B: 'ZplVeraMonoBold',
|
|
13
|
+
C: 'ZplPrintLabMono', D: 'ZplPrintLabMono', E: 'ZplOCRB',
|
|
14
|
+
F: 'ZplPrintLabMono', G: 'ZplPrintLabMono', H: 'ZplOCRA'
|
|
15
|
+
};
|
|
16
|
+
const documentLoads = new WeakMap();
|
|
17
|
+
const deviceMagnification = (requested, step) => Math.min(10, Math.max(1, Math.round(requested / step)));
|
|
18
|
+
/** Resolve the printer's discrete bitmap-font magnifications. Width 0 means
|
|
19
|
+
* use the height magnification, rather than a zero-width glyph cell. */
|
|
20
|
+
export function getDeviceFontMagnification(font, height, width) {
|
|
21
|
+
if (font === '0' || !(height > 0))
|
|
22
|
+
return null;
|
|
23
|
+
const spec = deviceFonts[font];
|
|
24
|
+
const heightMagnification = deviceMagnification(height, spec.magStep);
|
|
25
|
+
return {
|
|
26
|
+
height: heightMagnification,
|
|
27
|
+
width: width > 0 ? deviceMagnification(width, spec.magWidthStep) : heightMagnification,
|
|
28
|
+
heightStep: spec.magStep,
|
|
29
|
+
widthStep: spec.magWidthStep
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
export async function loadZplFonts(document) {
|
|
33
|
+
let loading = documentLoads.get(document);
|
|
34
|
+
if (!loading) {
|
|
35
|
+
const sources = [
|
|
36
|
+
['ZplPrintLab', 'PrintLabZPL-Bold.woff2'],
|
|
37
|
+
['ZplPrintLabMono', 'PrintLabMono.ttf'],
|
|
38
|
+
['ZplVeraMono', 'VeraMono.ttf'],
|
|
39
|
+
['ZplVeraMonoBold', 'VeraMono-Bold.ttf'],
|
|
40
|
+
['ZplOCRB', 'OCRB.ttf'],
|
|
41
|
+
['ZplOCRA', 'OCRA.ttf']
|
|
42
|
+
];
|
|
43
|
+
loading = Promise.all(sources.map(async ([family, file]) => {
|
|
44
|
+
const url = new URL(`../assets/fonts/${file}`, import.meta.url);
|
|
45
|
+
const face = new FontFace(family, `url(${url})`);
|
|
46
|
+
await face.load();
|
|
47
|
+
document.fonts.add(face);
|
|
48
|
+
})).then(() => undefined);
|
|
49
|
+
documentLoads.set(document, loading);
|
|
50
|
+
}
|
|
51
|
+
await loading;
|
|
52
|
+
}
|
|
53
|
+
export function getDeviceFontMetrics(font, height, width) {
|
|
54
|
+
const magnification = getDeviceFontMagnification(font, height, width);
|
|
55
|
+
if (!magnification)
|
|
56
|
+
return null;
|
|
57
|
+
const spec = deviceFonts[font];
|
|
58
|
+
const fontSize = magnification.height * spec.capInkPerMag / spec.capPerEm;
|
|
59
|
+
const scaleX = magnification.width * spec.advancePerMag / (fontSize * spec.advancePerEm) * (spec.widthCorrection ?? 1);
|
|
60
|
+
return {
|
|
61
|
+
fontSize,
|
|
62
|
+
scaleX,
|
|
63
|
+
characterAdvance: magnification.width * spec.advancePerMag,
|
|
64
|
+
xOffset: (spec.xOffsetEm ?? 0) * fontSize,
|
|
65
|
+
yOffset: (spec.yOffsetEm ?? 0) * fontSize,
|
|
66
|
+
letterSpacing: (spec.letterSpacingEm ?? 0) * fontSize
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/** Numeric ZPL width that represents the natural cell at a given requested
|
|
70
|
+
* height. Used only when an automatic-width field is stretched
|
|
71
|
+
* disproportionately and therefore has to become explicit. */
|
|
72
|
+
export function getNaturalZplFontWidth(font, height) {
|
|
73
|
+
if (font === '0')
|
|
74
|
+
return Math.max(1, Math.round(height));
|
|
75
|
+
const spec = deviceFonts[font];
|
|
76
|
+
const magnificationHeight = deviceMagnification(height, spec.magStep);
|
|
77
|
+
return magnificationHeight * spec.magWidthStep;
|
|
78
|
+
}
|
|
79
|
+
export function applyDeviceFontCase(font, content) {
|
|
80
|
+
if (font === 'B')
|
|
81
|
+
return content.toUpperCase();
|
|
82
|
+
if (font === 'H')
|
|
83
|
+
return content.replace(/[a-z]/g, '');
|
|
84
|
+
return content;
|
|
85
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -5,12 +5,19 @@ export * from "./widgets/zpl-graphic-diagonal-line.js";
|
|
|
5
5
|
export * from "./widgets/zpl-image.js";
|
|
6
6
|
export * from "./widgets/zpl-text.js";
|
|
7
7
|
export * from "./monaco/ZplLanguage.js";
|
|
8
|
-
export * from "./extensions/ZplLayoutResizeExtensionProvider.js";
|
|
9
8
|
export * from "./services/ZplImageDrop.js";
|
|
10
9
|
export * from "./services/ZplLayoutCopyPasteService.js";
|
|
11
10
|
export * from "./services/ZplLayoutPlacementService.js";
|
|
12
11
|
export * from "./services/ZplParserService.js";
|
|
13
|
-
export * from "./
|
|
14
|
-
export * from "./
|
|
12
|
+
export * from "./services/ZplBarcodePropertiesService.js";
|
|
13
|
+
export * from "./services/ZplIncludeInOutputPropertiesService.js";
|
|
14
|
+
export * from "./services/ZplBarcodeElementsService.js";
|
|
15
|
+
export * from "./services/ZplElementResizeStrategy.js";
|
|
16
|
+
export * from "./services/ZplTextPropertiesService.js";
|
|
17
|
+
export * from "./services/ZplDiagonalLineExtension.js";
|
|
18
|
+
export * from "./services/ZplTextEditExtension.js";
|
|
19
|
+
export * from "./barcodes/barcodeRegistry.js";
|
|
20
|
+
export * from "./barcodes/bwipRenderer.js";
|
|
21
|
+
export * from "./fonts/zplFonts.js";
|
|
15
22
|
export * from "./setupZplServiceContainer.js";
|
|
16
23
|
export * from "./zplHelper.js";
|
package/dist/index.js
CHANGED
|
@@ -5,12 +5,19 @@ export * from "./widgets/zpl-graphic-diagonal-line.js";
|
|
|
5
5
|
export * from "./widgets/zpl-image.js";
|
|
6
6
|
export * from "./widgets/zpl-text.js";
|
|
7
7
|
export * from "./monaco/ZplLanguage.js";
|
|
8
|
-
export * from "./extensions/ZplLayoutResizeExtensionProvider.js";
|
|
9
8
|
export * from "./services/ZplImageDrop.js";
|
|
10
9
|
export * from "./services/ZplLayoutCopyPasteService.js";
|
|
11
10
|
export * from "./services/ZplLayoutPlacementService.js";
|
|
12
11
|
export * from "./services/ZplParserService.js";
|
|
13
|
-
export * from "./
|
|
14
|
-
export * from "./
|
|
12
|
+
export * from "./services/ZplBarcodePropertiesService.js";
|
|
13
|
+
export * from "./services/ZplIncludeInOutputPropertiesService.js";
|
|
14
|
+
export * from "./services/ZplBarcodeElementsService.js";
|
|
15
|
+
export * from "./services/ZplElementResizeStrategy.js";
|
|
16
|
+
export * from "./services/ZplTextPropertiesService.js";
|
|
17
|
+
export * from "./services/ZplDiagonalLineExtension.js";
|
|
18
|
+
export * from "./services/ZplTextEditExtension.js";
|
|
19
|
+
export * from "./barcodes/barcodeRegistry.js";
|
|
20
|
+
export * from "./barcodes/bwipRenderer.js";
|
|
21
|
+
export * from "./fonts/zplFonts.js";
|
|
15
22
|
export * from "./setupZplServiceContainer.js";
|
|
16
23
|
export * from "./zplHelper.js";
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { attributesForBarcode, barcodeRegistry, barcodeTypes } from '../barcodes/barcodeRegistry.js';
|
|
2
|
+
export class ZplBarcodeElementsService {
|
|
3
|
+
name = 'zpl barcodes';
|
|
4
|
+
async getElements() {
|
|
5
|
+
return barcodeTypes.map(type => {
|
|
6
|
+
const definition = barcodeRegistry[type];
|
|
7
|
+
return {
|
|
8
|
+
name: definition.label,
|
|
9
|
+
description: `ZPL ^${definition.command} ${definition.label}`,
|
|
10
|
+
tag: 'zpl-barcode',
|
|
11
|
+
type: 'webcomponent',
|
|
12
|
+
defaultAttributes: attributesForBarcode(type)
|
|
13
|
+
};
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { AbstractPropertiesService, IDesignItem, IProperty, RefreshMode } from '@node-projects/web-component-designer';
|
|
2
|
+
export declare class ZplBarcodePropertiesService extends AbstractPropertiesService {
|
|
3
|
+
name: string;
|
|
4
|
+
getRefreshMode(): RefreshMode;
|
|
5
|
+
isHandledElement(designItem: IDesignItem): boolean;
|
|
6
|
+
protected _notifyChangedProperty(designItem: IDesignItem): void;
|
|
7
|
+
getProperties(designItem: IDesignItem): Promise<IProperty[]>;
|
|
8
|
+
getValue(designItems: IDesignItem[], property: IProperty): any;
|
|
9
|
+
setValue(designItems: IDesignItem[], property: IProperty, value: any): Promise<void>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { AbstractPropertiesService, PropertyType, RefreshMode } from '@node-projects/web-component-designer';
|
|
2
|
+
import { getBarcodeDefinition } from '../barcodes/barcodeRegistry.js';
|
|
3
|
+
import { ZplBarcode } from '../widgets/zpl-barcode.js';
|
|
4
|
+
export class ZplBarcodePropertiesService extends AbstractPropertiesService {
|
|
5
|
+
name = 'zplBarcode';
|
|
6
|
+
getRefreshMode() {
|
|
7
|
+
return RefreshMode.fullOnValueChange;
|
|
8
|
+
}
|
|
9
|
+
isHandledElement(designItem) {
|
|
10
|
+
return designItem.element instanceof ZplBarcode;
|
|
11
|
+
}
|
|
12
|
+
_notifyChangedProperty(designItem) {
|
|
13
|
+
designItem.element.refreshFromAttributes();
|
|
14
|
+
}
|
|
15
|
+
async getProperties(designItem) {
|
|
16
|
+
const definition = getBarcodeDefinition(designItem.getAttribute('type'));
|
|
17
|
+
return definition.properties.map(property => ({
|
|
18
|
+
name: property.name,
|
|
19
|
+
displayName: property.name.replace(/[A-Z]/g, value => ` ${value.toLowerCase()}`),
|
|
20
|
+
attributeName: property.attributeName,
|
|
21
|
+
type: property.type === 'enum' ? 'enum' : property.type,
|
|
22
|
+
min: property.min,
|
|
23
|
+
max: property.max,
|
|
24
|
+
step: property.step,
|
|
25
|
+
enumValues: property.values?.map(value => [value, value]),
|
|
26
|
+
defaultValue: definition.defaults[property.name],
|
|
27
|
+
service: this,
|
|
28
|
+
propertyType: PropertyType.attribute
|
|
29
|
+
}));
|
|
30
|
+
}
|
|
31
|
+
getValue(designItems, property) {
|
|
32
|
+
if (property.type !== 'boolean')
|
|
33
|
+
return super.getValue(designItems, property);
|
|
34
|
+
const value = designItems[0]?.getAttribute(property.attributeName ?? property.name);
|
|
35
|
+
if (value == null)
|
|
36
|
+
return property.defaultValue ?? false;
|
|
37
|
+
return value === '' || value === 'true' || value === 'Y';
|
|
38
|
+
}
|
|
39
|
+
async setValue(designItems, property, value) {
|
|
40
|
+
if (property.type !== 'boolean')
|
|
41
|
+
return super.setValue(designItems, property, value);
|
|
42
|
+
if (designItems.length === 0)
|
|
43
|
+
return;
|
|
44
|
+
const group = designItems[0].openGroup(`property changed: ${property.name} to ${value}`);
|
|
45
|
+
try {
|
|
46
|
+
for (const designItem of designItems) {
|
|
47
|
+
if (this.isHandledElement(designItem)) {
|
|
48
|
+
designItem.setAttribute(property.attributeName ?? property.name, value ? 'true' : 'false');
|
|
49
|
+
this._notifyChangedProperty(designItem);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
group.commit();
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
group.abort();
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { AbstractExtension, IDesignItem, IDesignerCanvas, IDesignerExtension, IDesignerExtensionProvider, IExtensionManager, IPoint } from '@node-projects/web-component-designer';
|
|
2
|
+
export type ZplDiagonalOrientation = 'L' | 'R';
|
|
3
|
+
export interface ZplDiagonalEndpoints {
|
|
4
|
+
first: IPoint;
|
|
5
|
+
second: IPoint;
|
|
6
|
+
}
|
|
7
|
+
export interface ZplDiagonalResizeResult {
|
|
8
|
+
endpoints: ZplDiagonalEndpoints;
|
|
9
|
+
left: number;
|
|
10
|
+
top: number;
|
|
11
|
+
width: number;
|
|
12
|
+
height: number;
|
|
13
|
+
orientation: ZplDiagonalOrientation;
|
|
14
|
+
}
|
|
15
|
+
export declare function getZplDiagonalVisibleHeight(elementHeight: number, lineStrokeWidth: number): number;
|
|
16
|
+
export declare function getZplDiagonalElementHeight(visibleHeight: number, lineStrokeWidth: number): number;
|
|
17
|
+
export declare function getZplDiagonalEndpoints(left: number, top: number, width: number, height: number, orientation: ZplDiagonalOrientation): ZplDiagonalEndpoints;
|
|
18
|
+
/**
|
|
19
|
+
* Moves one line endpoint by a gesture delta. The other endpoint is copied
|
|
20
|
+
* unchanged; bounds and ZPL orientation are derived from the resulting line.
|
|
21
|
+
*/
|
|
22
|
+
export declare function resizeZplDiagonalEndpoint(original: ZplDiagonalEndpoints, draggedEndpoint: 0 | 1, delta: IPoint, fallbackOrientation: ZplDiagonalOrientation): ZplDiagonalResizeResult;
|
|
23
|
+
export declare class ZplDiagonalLineExtension extends AbstractExtension {
|
|
24
|
+
private _firstHandle?;
|
|
25
|
+
private _secondHandle?;
|
|
26
|
+
private _dragState?;
|
|
27
|
+
private readonly _eventCleanups;
|
|
28
|
+
constructor(extensionManager: IExtensionManager, designerCanvas: IDesignerCanvas, extendedItem: IDesignItem);
|
|
29
|
+
extend(): void;
|
|
30
|
+
refresh(): void;
|
|
31
|
+
dispose(): void;
|
|
32
|
+
private _drawHandle;
|
|
33
|
+
private _pointerDown;
|
|
34
|
+
private _pointerMove;
|
|
35
|
+
private _pointerUp;
|
|
36
|
+
private _pointerCancel;
|
|
37
|
+
private _commit;
|
|
38
|
+
private _restore;
|
|
39
|
+
private _readCssEndpoints;
|
|
40
|
+
private _readOverlayEndpoints;
|
|
41
|
+
}
|
|
42
|
+
export declare class ZplDiagonalLineExtensionProvider implements IDesignerExtensionProvider {
|
|
43
|
+
shouldExtend(_extensionManager: IExtensionManager, designerCanvas: IDesignerCanvas, designItem: IDesignItem): boolean;
|
|
44
|
+
getExtension(extensionManager: IExtensionManager, designerCanvas: IDesignerCanvas, designItem: IDesignItem): IDesignerExtension;
|
|
45
|
+
readonly style: CSSStyleSheet;
|
|
46
|
+
}
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import { css } from '@node-projects/base-custom-webcomponent';
|
|
2
|
+
import { AbstractExtension, EventNames, OverlayLayer } from '@node-projects/web-component-designer';
|
|
3
|
+
import { ZplGraphicDiagonalLine } from '../widgets/zpl-graphic-diagonal-line.js';
|
|
4
|
+
export function getZplDiagonalVisibleHeight(elementHeight, lineStrokeWidth) {
|
|
5
|
+
return Math.max(0, elementHeight - Math.max(0, lineStrokeWidth));
|
|
6
|
+
}
|
|
7
|
+
export function getZplDiagonalElementHeight(visibleHeight, lineStrokeWidth) {
|
|
8
|
+
return Math.max(0, visibleHeight) + Math.max(0, lineStrokeWidth);
|
|
9
|
+
}
|
|
10
|
+
function orientationForEndpoints(endpoints, fallback) {
|
|
11
|
+
const dx = endpoints.second.x - endpoints.first.x;
|
|
12
|
+
const dy = endpoints.second.y - endpoints.first.y;
|
|
13
|
+
if (dx === 0 || dy === 0)
|
|
14
|
+
return fallback;
|
|
15
|
+
return dx * dy > 0 ? 'L' : 'R';
|
|
16
|
+
}
|
|
17
|
+
export function getZplDiagonalEndpoints(left, top, width, height, orientation) {
|
|
18
|
+
return orientation === 'R'
|
|
19
|
+
? { first: { x: left, y: top + height }, second: { x: left + width, y: top } }
|
|
20
|
+
: { first: { x: left, y: top }, second: { x: left + width, y: top + height } };
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Moves one line endpoint by a gesture delta. The other endpoint is copied
|
|
24
|
+
* unchanged; bounds and ZPL orientation are derived from the resulting line.
|
|
25
|
+
*/
|
|
26
|
+
export function resizeZplDiagonalEndpoint(original, draggedEndpoint, delta, fallbackOrientation) {
|
|
27
|
+
const endpoints = {
|
|
28
|
+
first: { ...original.first },
|
|
29
|
+
second: { ...original.second }
|
|
30
|
+
};
|
|
31
|
+
const dragged = draggedEndpoint === 0 ? endpoints.first : endpoints.second;
|
|
32
|
+
const originalDragged = draggedEndpoint === 0 ? original.first : original.second;
|
|
33
|
+
dragged.x = originalDragged.x + delta.x;
|
|
34
|
+
dragged.y = originalDragged.y + delta.y;
|
|
35
|
+
const left = Math.min(endpoints.first.x, endpoints.second.x);
|
|
36
|
+
const top = Math.min(endpoints.first.y, endpoints.second.y);
|
|
37
|
+
return {
|
|
38
|
+
endpoints,
|
|
39
|
+
left,
|
|
40
|
+
top,
|
|
41
|
+
width: Math.abs(endpoints.second.x - endpoints.first.x),
|
|
42
|
+
height: Math.abs(endpoints.second.y - endpoints.first.y),
|
|
43
|
+
orientation: orientationForEndpoints(endpoints, fallbackOrientation)
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function numericStyle(element, name) {
|
|
47
|
+
const inline = parseFloat(element.style.getPropertyValue(name));
|
|
48
|
+
if (Number.isFinite(inline))
|
|
49
|
+
return inline;
|
|
50
|
+
const computed = parseFloat(getComputedStyle(element).getPropertyValue(name));
|
|
51
|
+
return Number.isFinite(computed) ? computed : 0;
|
|
52
|
+
}
|
|
53
|
+
function strokeWidth(element) {
|
|
54
|
+
const value = Number(element.getAttribute('stroke-width'));
|
|
55
|
+
return Number.isFinite(value) ? Math.max(0, value) : 1;
|
|
56
|
+
}
|
|
57
|
+
function px(value) {
|
|
58
|
+
return `${Math.round(value)}px`;
|
|
59
|
+
}
|
|
60
|
+
export class ZplDiagonalLineExtension extends AbstractExtension {
|
|
61
|
+
_firstHandle;
|
|
62
|
+
_secondHandle;
|
|
63
|
+
_dragState;
|
|
64
|
+
_eventCleanups = [];
|
|
65
|
+
constructor(extensionManager, designerCanvas, extendedItem) {
|
|
66
|
+
super(extensionManager, designerCanvas, extendedItem);
|
|
67
|
+
}
|
|
68
|
+
extend() {
|
|
69
|
+
this.refresh();
|
|
70
|
+
}
|
|
71
|
+
refresh() {
|
|
72
|
+
const endpoints = this._dragState?.overlayEndpoints ?? this._readOverlayEndpoints();
|
|
73
|
+
const radius = 5 / this.designerCanvas.scaleFactor;
|
|
74
|
+
this._firstHandle = this._drawHandle(endpoints.first, 0, radius, this._firstHandle);
|
|
75
|
+
this._secondHandle = this._drawHandle(endpoints.second, 1, radius, this._secondHandle);
|
|
76
|
+
}
|
|
77
|
+
dispose() {
|
|
78
|
+
for (const cleanup of this._eventCleanups)
|
|
79
|
+
cleanup();
|
|
80
|
+
this._eventCleanups.length = 0;
|
|
81
|
+
this._removeAllOverlays();
|
|
82
|
+
this._firstHandle = undefined;
|
|
83
|
+
this._secondHandle = undefined;
|
|
84
|
+
this._dragState = undefined;
|
|
85
|
+
}
|
|
86
|
+
_drawHandle(point, endpoint, radius, oldHandle) {
|
|
87
|
+
const handle = this._drawCircle(point.x, point.y, radius, 'svg-zpl-line-endpoint', oldHandle, OverlayLayer.Foreground);
|
|
88
|
+
handle.style.strokeWidth = `${1 / this.designerCanvas.zoomFactor}`;
|
|
89
|
+
handle.style.cursor = 'move';
|
|
90
|
+
if (!oldHandle) {
|
|
91
|
+
const down = (event) => this._pointerDown(event, handle, endpoint);
|
|
92
|
+
const move = (event) => this._pointerMove(event, endpoint);
|
|
93
|
+
const up = (event) => this._pointerUp(event);
|
|
94
|
+
const cancel = (event) => this._pointerCancel(event);
|
|
95
|
+
handle.addEventListener(EventNames.PointerDown, down);
|
|
96
|
+
handle.addEventListener(EventNames.PointerMove, move);
|
|
97
|
+
handle.addEventListener(EventNames.PointerUp, up);
|
|
98
|
+
handle.addEventListener('pointercancel', cancel);
|
|
99
|
+
this._eventCleanups.push(() => handle.removeEventListener(EventNames.PointerDown, down), () => handle.removeEventListener(EventNames.PointerMove, move), () => handle.removeEventListener(EventNames.PointerUp, up), () => handle.removeEventListener('pointercancel', cancel));
|
|
100
|
+
}
|
|
101
|
+
return handle;
|
|
102
|
+
}
|
|
103
|
+
_pointerDown(event, handle, endpoint) {
|
|
104
|
+
event.stopPropagation();
|
|
105
|
+
event.preventDefault();
|
|
106
|
+
handle.setPointerCapture(event.pointerId);
|
|
107
|
+
const element = this.extendedItem.element;
|
|
108
|
+
this._dragState = {
|
|
109
|
+
draggedEndpoint: endpoint,
|
|
110
|
+
startPointer: this.designerCanvas.getNormalizedEventCoordinates(event),
|
|
111
|
+
originalCssEndpoints: this._readCssEndpoints(),
|
|
112
|
+
originalOverlayEndpoints: this._readOverlayEndpoints(),
|
|
113
|
+
overlayEndpoints: this._readOverlayEndpoints(),
|
|
114
|
+
originalStyles: {
|
|
115
|
+
left: element.style.left,
|
|
116
|
+
top: element.style.top,
|
|
117
|
+
right: element.style.right,
|
|
118
|
+
bottom: element.style.bottom,
|
|
119
|
+
width: element.style.width,
|
|
120
|
+
height: element.style.height
|
|
121
|
+
},
|
|
122
|
+
originalOrientation: element.getAttribute('orientation'),
|
|
123
|
+
changed: false
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
_pointerMove(event, endpoint) {
|
|
127
|
+
event.stopPropagation();
|
|
128
|
+
event.preventDefault();
|
|
129
|
+
const state = this._dragState;
|
|
130
|
+
if (!state || state.draggedEndpoint !== endpoint || event.buttons === 0)
|
|
131
|
+
return;
|
|
132
|
+
const current = this.designerCanvas.getNormalizedEventCoordinates(event);
|
|
133
|
+
const delta = { x: current.x - state.startPointer.x, y: current.y - state.startPointer.y };
|
|
134
|
+
if (event.shiftKey) {
|
|
135
|
+
if (Math.abs(delta.x) >= Math.abs(delta.y))
|
|
136
|
+
delta.y = 0;
|
|
137
|
+
else
|
|
138
|
+
delta.x = 0;
|
|
139
|
+
}
|
|
140
|
+
const orientation = state.originalOrientation === 'R' ? 'R' : 'L';
|
|
141
|
+
const result = resizeZplDiagonalEndpoint(state.originalCssEndpoints, endpoint, delta, orientation);
|
|
142
|
+
const roundedDelta = {
|
|
143
|
+
x: Math.round((endpoint === 0 ? result.endpoints.first : result.endpoints.second).x)
|
|
144
|
+
- (endpoint === 0 ? state.originalCssEndpoints.first.x : state.originalCssEndpoints.second.x),
|
|
145
|
+
y: Math.round((endpoint === 0 ? result.endpoints.first : result.endpoints.second).y)
|
|
146
|
+
- (endpoint === 0 ? state.originalCssEndpoints.first.y : state.originalCssEndpoints.second.y)
|
|
147
|
+
};
|
|
148
|
+
state.overlayEndpoints = {
|
|
149
|
+
first: { ...state.originalOverlayEndpoints.first },
|
|
150
|
+
second: { ...state.originalOverlayEndpoints.second }
|
|
151
|
+
};
|
|
152
|
+
const overlayDragged = endpoint === 0 ? state.overlayEndpoints.first : state.overlayEndpoints.second;
|
|
153
|
+
const originalOverlayDragged = endpoint === 0 ? state.originalOverlayEndpoints.first : state.originalOverlayEndpoints.second;
|
|
154
|
+
overlayDragged.x = originalOverlayDragged.x + roundedDelta.x;
|
|
155
|
+
overlayDragged.y = originalOverlayDragged.y + roundedDelta.y;
|
|
156
|
+
const element = this.extendedItem.element;
|
|
157
|
+
element.style.removeProperty('right');
|
|
158
|
+
element.style.removeProperty('bottom');
|
|
159
|
+
element.style.left = px(result.left);
|
|
160
|
+
element.style.top = px(result.top);
|
|
161
|
+
element.style.width = px(result.width);
|
|
162
|
+
// ZplGraphicDiagonalLine keeps its stroke inside the host by drawing
|
|
163
|
+
// the lower SVG endpoint at `height - strokeWidth`.
|
|
164
|
+
element.style.height = px(getZplDiagonalElementHeight(result.height, strokeWidth(element)));
|
|
165
|
+
element.setAttribute('orientation', result.orientation);
|
|
166
|
+
state.changed = true;
|
|
167
|
+
this.refresh();
|
|
168
|
+
this.extensionManager.refreshAllExtensions([this.extendedItem, this.extendedItem.parent], this);
|
|
169
|
+
this.designerCanvas.raiseDesignItemsChanged([this.extendedItem], 'resize', false);
|
|
170
|
+
}
|
|
171
|
+
_pointerUp(event) {
|
|
172
|
+
event.stopPropagation();
|
|
173
|
+
event.target.releasePointerCapture(event.pointerId);
|
|
174
|
+
const state = this._dragState;
|
|
175
|
+
if (!state)
|
|
176
|
+
return;
|
|
177
|
+
if (state.changed)
|
|
178
|
+
this._commit(state);
|
|
179
|
+
this._dragState = undefined;
|
|
180
|
+
this.refresh();
|
|
181
|
+
}
|
|
182
|
+
_pointerCancel(event) {
|
|
183
|
+
event.stopPropagation();
|
|
184
|
+
if (!this._dragState)
|
|
185
|
+
return;
|
|
186
|
+
this._restore(this._dragState);
|
|
187
|
+
this._dragState = undefined;
|
|
188
|
+
this.refresh();
|
|
189
|
+
this.extensionManager.refreshAllExtensions([this.extendedItem, this.extendedItem.parent], this);
|
|
190
|
+
}
|
|
191
|
+
_commit(state) {
|
|
192
|
+
const element = this.extendedItem.element;
|
|
193
|
+
const finalStyles = {
|
|
194
|
+
left: element.style.left,
|
|
195
|
+
top: element.style.top,
|
|
196
|
+
width: element.style.width === '0px' ? '1px' : element.style.width,
|
|
197
|
+
height: element.style.height === '0px' ? '1px' : element.style.height
|
|
198
|
+
};
|
|
199
|
+
const finalOrientation = element.getAttribute('orientation') ?? 'L';
|
|
200
|
+
this._restore(state);
|
|
201
|
+
const group = this.extendedItem.openGroup('Resize ZPL diagonal line');
|
|
202
|
+
try {
|
|
203
|
+
this.extendedItem.setStyle('right', null);
|
|
204
|
+
this.extendedItem.setStyle('bottom', null);
|
|
205
|
+
this.extendedItem.setStyle('left', finalStyles.left);
|
|
206
|
+
this.extendedItem.setStyle('top', finalStyles.top);
|
|
207
|
+
this.extendedItem.setStyle('width', finalStyles.width);
|
|
208
|
+
this.extendedItem.setStyle('height', finalStyles.height);
|
|
209
|
+
this.extendedItem.setAttribute('orientation', finalOrientation);
|
|
210
|
+
group.commit();
|
|
211
|
+
this.designerCanvas.raiseDesignItemsChanged([this.extendedItem], 'resize', true);
|
|
212
|
+
}
|
|
213
|
+
catch (error) {
|
|
214
|
+
group.abort();
|
|
215
|
+
throw error;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
_restore(state) {
|
|
219
|
+
const element = this.extendedItem.element;
|
|
220
|
+
for (const [name, value] of Object.entries(state.originalStyles)) {
|
|
221
|
+
if (value)
|
|
222
|
+
element.style.setProperty(name, value);
|
|
223
|
+
else
|
|
224
|
+
element.style.removeProperty(name);
|
|
225
|
+
}
|
|
226
|
+
if (state.originalOrientation == null)
|
|
227
|
+
element.removeAttribute('orientation');
|
|
228
|
+
else
|
|
229
|
+
element.setAttribute('orientation', state.originalOrientation);
|
|
230
|
+
}
|
|
231
|
+
_readCssEndpoints() {
|
|
232
|
+
const element = this.extendedItem.element;
|
|
233
|
+
return getZplDiagonalEndpoints(numericStyle(element, 'left'), numericStyle(element, 'top'), numericStyle(element, 'width'), getZplDiagonalVisibleHeight(numericStyle(element, 'height'), strokeWidth(element)), element.getAttribute('orientation') === 'R' ? 'R' : 'L');
|
|
234
|
+
}
|
|
235
|
+
_readOverlayEndpoints() {
|
|
236
|
+
const element = this.extendedItem.element;
|
|
237
|
+
const rect = this.designerCanvas.getNormalizedElementCoordinates(this.extendedItem.element);
|
|
238
|
+
return getZplDiagonalEndpoints(rect.x, rect.y, rect.width, getZplDiagonalVisibleHeight(rect.height, strokeWidth(element)), this.extendedItem.getAttribute('orientation') === 'R' ? 'R' : 'L');
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
export class ZplDiagonalLineExtensionProvider {
|
|
242
|
+
shouldExtend(_extensionManager, designerCanvas, designItem) {
|
|
243
|
+
return !designerCanvas.readOnly && designItem.element.localName === ZplGraphicDiagonalLine.is;
|
|
244
|
+
}
|
|
245
|
+
getExtension(extensionManager, designerCanvas, designItem) {
|
|
246
|
+
return new ZplDiagonalLineExtension(extensionManager, designerCanvas, designItem);
|
|
247
|
+
}
|
|
248
|
+
style = css `
|
|
249
|
+
.svg-zpl-line-endpoint {
|
|
250
|
+
stroke: #3899ec;
|
|
251
|
+
fill: white;
|
|
252
|
+
pointer-events: auto;
|
|
253
|
+
}
|
|
254
|
+
`;
|
|
255
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { ElementResizeContext, IDesignItem, IElementResizeStrategy, ISize, ResizeHandle } from '@node-projects/web-component-designer';
|
|
2
|
+
export { quantizeZplValue, zplAxisScales } from './zplResizeGeometry.js';
|
|
3
|
+
interface ResizeState {
|
|
4
|
+
originalAttributes: Map<string, string | null>;
|
|
5
|
+
previewAttributes: Map<string, string>;
|
|
6
|
+
originalWidth: string;
|
|
7
|
+
originalHeight: string;
|
|
8
|
+
}
|
|
9
|
+
export declare class ZplElementResizeStrategy implements IElementResizeStrategy {
|
|
10
|
+
isHandledElement(designItem: IDesignItem): boolean;
|
|
11
|
+
getEnabledHandles(designItem: IDesignItem): readonly ResizeHandle[];
|
|
12
|
+
begin(context: ElementResizeContext): ResizeState;
|
|
13
|
+
preview(context: ElementResizeContext, rawState: unknown): ISize;
|
|
14
|
+
commit(context: ElementResizeContext, rawState: unknown): void;
|
|
15
|
+
cancel(context: ElementResizeContext, rawState: unknown): void;
|
|
16
|
+
private _restoreRaw;
|
|
17
|
+
private _initialNumber;
|
|
18
|
+
private _textAttributes;
|
|
19
|
+
private _barcodeAttributes;
|
|
20
|
+
}
|