@hyperframes/core 0.10.4 → 0.10.7

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.
@@ -15,6 +15,10 @@ export type ComboEvent = Pick<KeyboardEvent, "key" | "altKey" | "ctrlKey" | "met
15
15
  code?: string;
16
16
  };
17
17
  export declare function matchCombo(e: ComboEvent, c: Combo): boolean;
18
+ export declare const isMacPlatform: () => boolean;
19
+ type Modifiers = Partial<Pick<Combo, "mod" | "meta" | "ctrl" | "alt" | "shift">>;
20
+ export declare function formatModifiers(m: Modifiers): string;
18
21
  export declare function formatCombo(c: Combo): string;
19
22
  /** Two combos clash when one press could satisfy both; Cmd and Ctrl both count as mod. */
20
23
  export declare const comboClashKey: (c: Combo) => string;
24
+ export {};
package/dist/keyCombo.js CHANGED
@@ -77,9 +77,15 @@ export function matchCombo(e, c) {
77
77
  const modsOk = c.mod ? e.metaKey || e.ctrlKey : !!e.metaKey === c.meta && !!e.ctrlKey === c.ctrl;
78
78
  return modsOk && (c.named ? namedMatches(e, c.key) : charMatches(e, c));
79
79
  }
80
+ export const isMacPlatform = () => typeof navigator !== "undefined" && /Mac|iPhone|iPad|iPod/i.test(navigator.platform);
81
+ export function formatModifiers(m) {
82
+ if (isMacPlatform())
83
+ return `${m.ctrl ? "⌃" : ""}${m.alt ? "⌥" : ""}${m.mod || m.meta ? "⌘" : ""}${m.shift ? "⇧" : ""}`;
84
+ return `${m.ctrl || m.mod ? "Ctrl+" : ""}${m.meta ? "Meta+" : ""}${m.alt ? "Alt+" : ""}${m.shift ? "Shift+" : ""}`;
85
+ }
80
86
  export function formatCombo(c) {
81
- const mods = `${c.ctrl ? "⌃" : ""}${c.alt ? "⌥" : ""}${c.mod || c.meta ? "⌘" : ""}${c.shift ? "⇧" : ""}`;
82
- return mods + (SYMBOL[c.key] ?? (c.named ? c.key : c.key.toUpperCase()));
87
+ const symbol = c.key === "Backspace" && !isMacPlatform() ? "Backspace" : SYMBOL[c.key];
88
+ return formatModifiers(c) + (symbol ?? (c.named ? c.key : c.key.toUpperCase()));
83
89
  }
84
90
  /** Two combos clash when one press could satisfy both; Cmd and Ctrl both count as mod. */
85
91
  export const comboClashKey = (c) => [c.mod || c.meta || c.ctrl, c.alt, c.shift, c.key].join("|");
@@ -18,11 +18,16 @@ export function isLottieAnimationLoaded(anim) {
18
18
  }
19
19
  const MEDIA_HAVE_FUTURE_DATA = 3;
20
20
  const MEDIA_NETWORK_NO_SOURCE = 3;
21
+ function isDeferredPreload(media) {
22
+ const preload = media.getAttribute("preload")?.toLowerCase();
23
+ return preload === "none" || preload === "metadata";
24
+ }
21
25
  /** Whether a preview still waits on a `<video>`/`<audio>` that cannot play through, or a Lottie still loading. */
22
26
  export function previewAssetsUnloaded(doc, lotties) {
23
27
  for (const el of doc.querySelectorAll("video, audio")) {
24
28
  const media = el;
25
29
  if (!media.error &&
30
+ !isDeferredPreload(media) &&
26
31
  media.networkState !== MEDIA_NETWORK_NO_SOURCE &&
27
32
  media.readyState < MEDIA_HAVE_FUTURE_DATA)
28
33
  return true;
@@ -77,6 +77,8 @@ let host = null;
77
77
  let plane = null;
78
78
  let placed = [];
79
79
  let looks = [];
80
+ let places = [];
81
+ let planeAt = "";
80
82
  export const isDragChromeHost = (element) => element === host;
81
83
  function build(node, into) {
82
84
  const element = node.tag === "svg" || node.tag === "path"
@@ -84,8 +86,11 @@ function build(node, into) {
84
86
  : document.createElement(node.tag);
85
87
  for (const [name, value] of Object.entries(node.attrs ?? {}))
86
88
  element.setAttribute(name, value);
89
+ writeStyle(element, node.look);
90
+ element.style.setProperty("pointer-events", "none", "important");
87
91
  placed.push(element);
88
92
  looks.push(node.look);
93
+ places.push({});
89
94
  into.appendChild(element);
90
95
  for (const child of node.children ?? [])
91
96
  build(child, element);
@@ -110,6 +115,48 @@ function writeStyle(element, props) {
110
115
  for (const [name, value] of Object.entries(props))
111
116
  element.style.setProperty(name, value);
112
117
  }
118
+ /** Place properties that overlap none of the others, so each can be rewritten alone after the look. */
119
+ const WRITTEN_ALONE = new Set([
120
+ "left",
121
+ "top",
122
+ "right",
123
+ "bottom",
124
+ "width",
125
+ "height",
126
+ "transform",
127
+ "transform-origin",
128
+ "opacity",
129
+ "clip-path",
130
+ "cursor",
131
+ "touch-action",
132
+ ]);
133
+ function writeNode(i, place) {
134
+ const element = placed[i];
135
+ element.removeAttribute("style");
136
+ writeStyle(element, looks[i]);
137
+ writeStyle(element, place);
138
+ element.style.setProperty("pointer-events", "none", "important");
139
+ }
140
+ /** Writes one property on its own; false when the browser took no value, which only a fresh draw resolves. */
141
+ function writeAlone(i, name, value) {
142
+ const { style } = placed[i];
143
+ style.removeProperty(name);
144
+ style.setProperty(name, value);
145
+ return style.getPropertyValue(name) !== "";
146
+ }
147
+ function writeMovedPlace(i, next) {
148
+ const last = places[i];
149
+ places[i] = next;
150
+ const names = Object.keys(next);
151
+ const sameNames = names.length === Object.keys(last).length && names.every((name) => name in last);
152
+ const moved = names.filter((name) => last[name] !== next[name]);
153
+ if (sameNames && !moved.length && names.join() === Object.keys(last).join())
154
+ return;
155
+ if (!sameNames || !names.every((name) => WRITTEN_ALONE.has(name)))
156
+ return writeNode(i, next);
157
+ if (!moved.every((name) => writeAlone(i, name, next[name])))
158
+ writeNode(i, next);
159
+ }
113
160
  /** Mounts, places or clears the chrome in the drafts' turn; false when this place fits no mounted copy. */
114
161
  export function drawDragChrome(chrome) {
115
162
  if (chrome === null) {
@@ -121,13 +168,10 @@ export function drawDragChrome(chrome) {
121
168
  if (!plane || chrome.place.length !== placed.length)
122
169
  return false;
123
170
  const { origin, scale } = chrome;
124
- plane.style.transform = `scale(${1 / scale.x}, ${1 / scale.y}) translate(${-origin.x}px, ${-origin.y}px)`;
125
- placed.forEach((element, i) => {
126
- element.removeAttribute("style");
127
- writeStyle(element, looks[i]);
128
- writeStyle(element, chrome.place[i]);
129
- element.style.setProperty("pointer-events", "none", "important");
130
- });
171
+ const transform = `scale(${1 / scale.x}, ${1 / scale.y}) translate(${-origin.x}px, ${-origin.y}px)`;
172
+ if (planeAt !== transform)
173
+ plane.style.setProperty("transform", (planeAt = transform));
174
+ chrome.place.forEach((next, i) => writeMovedPlace(i, next));
131
175
  return true;
132
176
  }
133
177
  export function clearDragChrome() {
@@ -135,6 +179,8 @@ export function clearDragChrome() {
135
179
  host = plane = null;
136
180
  placed = [];
137
181
  looks = [];
182
+ places = [];
183
+ planeAt = "";
138
184
  }
139
185
  /** Runs `read` with the chrome host out of the document, so nothing it reads carries it. */
140
186
  export function withoutDragChrome(read) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyperframes/core",
3
- "version": "0.10.4",
3
+ "version": "0.10.7",
4
4
  "description": "",
5
5
  "repository": {
6
6
  "type": "git",
@@ -525,9 +525,9 @@
525
525
  "linkedom": "^0.18.12",
526
526
  "postcss": "^8.5.8",
527
527
  "wawoff2": "^2.0.1",
528
- "@hyperframes/lint": "0.10.4",
529
- "@hyperframes/studio-server": "0.10.4",
530
- "@hyperframes/parsers": "0.10.4"
528
+ "@hyperframes/parsers": "0.10.7",
529
+ "@hyperframes/studio-server": "0.10.7",
530
+ "@hyperframes/lint": "0.10.7"
531
531
  },
532
532
  "devDependencies": {
533
533
  "@fontsource/archivo-black": "^5.2.8",