@peter.prib/renderengine 0.2.1 → 0.2.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peter.prib/renderengine",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "ArchiMate modeller, diagram editor and chart library for the browser, with no runtime dependencies.",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -13,6 +13,17 @@ export class BaseRenderer {
13
13
  this.width = width;
14
14
  this.height = height;
15
15
  }
16
+ /**
17
+ * Take a new drawing size — the container's, once it has one. A renderer
18
+ * is sized from its container when it is created, and a container that is
19
+ * hidden then (a pane in a tab not yet shown) measures 0×0; without this
20
+ * the drawing stays 0×0 when the pane is shown. Subclasses resize their
21
+ * surface and call this.
22
+ */
23
+ resize(width, height) {
24
+ this.width = width;
25
+ this.height = height;
26
+ }
16
27
  clear() { throw new Error("Method 'clear()' must be implemented."); }
17
28
  rect(x, y, width, height, fill, stroke) { throw new Error("Method 'rect()' must be implemented."); }
18
29
  circle(cx, cy, r, fill, stroke) { throw new Error("Method 'circle()' must be implemented."); }
@@ -22,6 +22,13 @@ export class CanvasRenderer extends BaseRenderer {
22
22
  this.ctx = this.canvas.getContext('2d');
23
23
  }
24
24
 
25
+ /** Resize the canvas. Setting a canvas's size clears it, so redraw after. */
26
+ resize(width, height) {
27
+ super.resize(width, height);
28
+ this.canvas.width = width;
29
+ this.canvas.height = height;
30
+ }
31
+
25
32
  clear() {
26
33
  this.ctx.clearRect(0, 0, this.width, this.height);
27
34
  this._shapes = [];
@@ -167,6 +167,22 @@ export class SVGRenderer extends BaseRenderer {
167
167
  this.container.appendChild(this.svg);
168
168
  }
169
169
 
170
+ /**
171
+ * Resize the <svg>. Its viewBox follows only while it is still the size it
172
+ * was created with: a diagram sets its own viewBox to its content, and that
173
+ * must survive, so the diagram then scales to the new box instead of
174
+ * being cropped to it.
175
+ */
176
+ resize(width, height) {
177
+ const defaultViewBox = `0 0 ${this.width} ${this.height}`;
178
+ super.resize(width, height);
179
+ this.svg.setAttribute('width', width);
180
+ this.svg.setAttribute('height', height);
181
+ if (this.svg.getAttribute('viewBox') === defaultViewBox) {
182
+ this.svg.setAttribute('viewBox', `0 0 ${width} ${height}`);
183
+ }
184
+ }
185
+
170
186
  clear() {
171
187
  this._elMap.clear();
172
188
  // Keep <defs> and <style> elements; remove everything else.
@@ -48,6 +48,21 @@ export class PopupMenu {
48
48
  };
49
49
  }
50
50
 
51
+ /**
52
+ * Where a submenu opens beside its item (the item's box `{left, right, top}`),
53
+ * to be all inside the window: to the right, or to the left when the right
54
+ * would pass the edge (and there is more room on the left); level with the
55
+ * item, or lifted by as much as it would pass the bottom. `top` is relative
56
+ * to the item, as the submenu is positioned.
57
+ */
58
+ static submenuPlacement(item, width, height, windowWidth, windowHeight, margin = 4) {
59
+ const fitsRight = item.right + width + margin <= windowWidth;
60
+ const side = fitsRight || item.left < windowWidth - item.right ? 'right' : 'left';
61
+ const top = -4;
62
+ const overflow = item.top + top + height + margin - windowHeight;
63
+ return { side, top: overflow > 0 ? Math.max(top - overflow, margin - item.top) : top };
64
+ }
65
+
51
66
  static close() {
52
67
  const open = PopupMenu._open;
53
68
  if (!open) return;
@@ -85,7 +100,16 @@ export class PopupMenu {
85
100
  ...base,
86
101
  attrs: { ...base.attrs, 'aria-haspopup': 'menu' },
87
102
  on: {
88
- mouseenter: (e) => { e.currentTarget.style.background = hover; sub.style.display = 'block'; },
103
+ mouseenter: (e) => {
104
+ e.currentTarget.style.background = hover;
105
+ sub.style.display = 'block';
106
+ // Opened near the window's right or bottom edge, it opens left, or lifts.
107
+ const win = e.currentTarget.ownerDocument.defaultView;
108
+ const box = sub.getBoundingClientRect?.();
109
+ if (!win || !box?.width) return;
110
+ const at = PopupMenu.submenuPlacement(e.currentTarget.getBoundingClientRect(), box.width, box.height, win.innerWidth, win.innerHeight);
111
+ Object.assign(sub.style, at.side === 'left' ? { left: 'auto', right: '100%' } : { left: '100%', right: 'auto' }, { top: `${at.top}px` });
112
+ },
89
113
  mouseleave: (e) => { e.currentTarget.style.background = ''; sub.style.display = 'none'; },
90
114
  click: (e) => e.stopPropagation(),
91
115
  },