phoenix_live_view 1.1.30 → 1.2.0-rc.1

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.
Files changed (35) hide show
  1. package/assets/js/phoenix_live_view/constants.js +1 -0
  2. package/assets/js/phoenix_live_view/hooks.js +3 -5
  3. package/assets/js/phoenix_live_view/index.ts +36 -3
  4. package/assets/js/phoenix_live_view/js.js +3 -2
  5. package/assets/js/phoenix_live_view/js_commands.ts +16 -7
  6. package/assets/js/phoenix_live_view/live_socket.js +1 -1
  7. package/assets/js/phoenix_live_view/live_uploader.js +3 -2
  8. package/assets/js/phoenix_live_view/view.js +108 -88
  9. package/assets/js/phoenix_live_view/view_hook.ts +2 -2
  10. package/package.json +2 -2
  11. package/priv/static/phoenix_live_view.cjs.js +90 -76
  12. package/priv/static/phoenix_live_view.cjs.js.map +3 -3
  13. package/priv/static/phoenix_live_view.esm.js +90 -76
  14. package/priv/static/phoenix_live_view.esm.js.map +3 -3
  15. package/priv/static/phoenix_live_view.js +90 -76
  16. package/priv/static/phoenix_live_view.min.js +6 -6
  17. package/assets/js/types/aria.d.ts +0 -9
  18. package/assets/js/types/browser.d.ts +0 -15
  19. package/assets/js/types/constants.d.ts +0 -97
  20. package/assets/js/types/dom.d.ts +0 -63
  21. package/assets/js/types/dom_patch.d.ts +0 -53
  22. package/assets/js/types/dom_post_morph_restorer.d.ts +0 -8
  23. package/assets/js/types/element_ref.d.ts +0 -15
  24. package/assets/js/types/entry_uploader.d.ts +0 -16
  25. package/assets/js/types/hooks.d.ts +0 -10
  26. package/assets/js/types/index.d.ts +0 -313
  27. package/assets/js/types/js.d.ts +0 -99
  28. package/assets/js/types/js_commands.d.ts +0 -217
  29. package/assets/js/types/live_socket.d.ts +0 -149
  30. package/assets/js/types/live_uploader.d.ts +0 -29
  31. package/assets/js/types/rendered.d.ts +0 -49
  32. package/assets/js/types/upload_entry.d.ts +0 -42
  33. package/assets/js/types/utils.d.ts +0 -14
  34. package/assets/js/types/view.d.ts +0 -151
  35. package/assets/js/types/view_hook.d.ts +0 -233
@@ -85,6 +85,7 @@ export const PHX_LV_PID = "data-phx-pid";
85
85
  export const PHX_KEY = "key";
86
86
  export const PHX_PRIVATE = "phxPrivate";
87
87
  export const PHX_AUTO_RECOVER = "auto-recover";
88
+ export const PHX_NO_UNUSED_FIELD = "no-unused-field";
88
89
  export const PHX_LV_DEBUG = "phx:live-socket:debug";
89
90
  export const PHX_LV_PROFILE = "phx:live-socket:profiling";
90
91
  export const PHX_LV_LATENCY_SIM = "phx:live-socket:latency-sim";
@@ -46,10 +46,8 @@ const Hooks = {
46
46
  this.inputEl = document.getElementById(
47
47
  this.el.getAttribute(PHX_UPLOAD_REF),
48
48
  );
49
- LiveUploader.getEntryDataURL(this.inputEl, this.ref, (url) => {
50
- this.url = url;
51
- this.el.src = url;
52
- });
49
+ this.url = LiveUploader.getEntryDataURL(this.inputEl, this.ref);
50
+ this.el.src = this.url;
53
51
  },
54
52
  destroyed() {
55
53
  URL.revokeObjectURL(this.url);
@@ -264,7 +262,7 @@ Hooks.InfiniteScroll = {
264
262
  updated() {
265
263
  // Check if the scroll container still exists
266
264
  // https://github.com/phoenixframework/phoenix_live_view/issues/4169.
267
- if (this.scrollContainer && !this.scrollContainer.isConnected) {
265
+ if (!this.scrollContainer.isConnected) {
268
266
  this.destroyed();
269
267
  this.mounted();
270
268
  }
@@ -12,9 +12,10 @@ import { ViewHook } from "./view_hook";
12
12
  import View from "./view";
13
13
  import { logError } from "./utils";
14
14
 
15
- import type { LiveSocketJSCommands } from "./js_commands";
15
+ import type { EncodedJS, LiveSocketJSCommands } from "./js_commands";
16
16
  import type { Hook, HooksOptions } from "./view_hook";
17
17
  import type { Socket as PhoenixSocket } from "phoenix";
18
+ import LiveUploader from "./live_uploader";
18
19
 
19
20
  /**
20
21
  * Options for configuring the LiveSocket instance.
@@ -266,7 +267,11 @@ export interface LiveSocketInstanceInterface {
266
267
  *
267
268
  * See [`Phoenix.LiveView.JS`](https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.JS.html) for more information.
268
269
  */
269
- execJS(el: HTMLElement, encodedJS: string, eventType?: string | null): void;
270
+ execJS(
271
+ el: HTMLElement,
272
+ encodedJS: EncodedJS,
273
+ eventType?: string | null,
274
+ ): void;
270
275
  /**
271
276
  * Returns an object with methods to manipulate the DOM and execute JavaScript.
272
277
  * The applied changes integrate with server DOM patching.
@@ -346,4 +351,32 @@ function createHook(el: HTMLElement, callbacks: Hook): ViewHook {
346
351
  return hook;
347
352
  }
348
353
 
349
- export { LiveSocket, isUsedInput, createHook, ViewHook, Hook, HooksOptions };
354
+ /** Returns an object URL for the file matching the given upload ref,
355
+ * or `null` if no matching file is found.
356
+ *
357
+ * @param input - The file input element associated with the upload.
358
+ * @param uploadRef - The upload ref identifying the file entry.
359
+ *
360
+ * @example
361
+ *
362
+ * import { getFileURLForUpload } from "phoenix_live_view"
363
+ *
364
+ * let url = getFileURLForUpload(inputEl, uploadRef)
365
+ * if (url) { imgEl.src = url }
366
+ */
367
+ function getFileURLForUpload(
368
+ input: HTMLElement,
369
+ uploadRef: string,
370
+ ): string | null {
371
+ return LiveUploader.getEntryDataURL(input, uploadRef);
372
+ }
373
+
374
+ export {
375
+ LiveSocket,
376
+ isUsedInput,
377
+ createHook,
378
+ ViewHook,
379
+ Hook,
380
+ HooksOptions,
381
+ getFileURLForUpload,
382
+ };
@@ -11,8 +11,9 @@ const JS = {
11
11
  null,
12
12
  { callback: defaults && defaults.callback },
13
13
  ];
14
- const commands =
15
- phxEvent.charAt(0) === "["
14
+ const commands = Array.isArray(phxEvent)
15
+ ? phxEvent
16
+ : typeof phxEvent === "string" && phxEvent.startsWith("[")
16
17
  ? JSON.parse(phxEvent)
17
18
  : [[defaultKind, defaultArgs]];
18
19
 
@@ -1,6 +1,15 @@
1
1
  import JS from "./js";
2
2
  import LiveSocket from "./live_socket";
3
3
 
4
+ /**
5
+ * An encoded JS command. Use functions in the `Phoenix.LiveView.JS` module on
6
+ * the server to create and compose JS commands.
7
+ *
8
+ * The underlying primitive type is considered opaque, and may change in future
9
+ * versions.
10
+ */
11
+ export type EncodedJS = string | Array<any>;
12
+
4
13
  type Transition = string | string[];
5
14
 
6
15
  // Base options for commands involving transitions and timing
@@ -76,13 +85,13 @@ type NavigationOpts = {
76
85
  */
77
86
  interface AllJSCommands {
78
87
  /**
79
- * Executes encoded JavaScript in the context of the element.
88
+ * Executes an encoded JS command in the context of an element.
80
89
  * This version is for general use via liveSocket.js().
81
90
  *
82
- * @param el - The element in whose context to execute the JavaScript.
83
- * @param encodedJS - The encoded JavaScript string to execute.
91
+ * @param el - The element in whose context to execute the JS command.
92
+ * @param encodedJS - The encoded JS command with operations to execute.
84
93
  */
85
- exec(el: HTMLElement, encodedJS: string): void;
94
+ exec(el: HTMLElement, encodedJS: EncodedJS): void;
86
95
 
87
96
  /**
88
97
  * Shows an element.
@@ -382,9 +391,9 @@ export type LiveSocketJSCommands = AllJSCommands;
382
391
  */
383
392
  export interface HookJSCommands extends Omit<AllJSCommands, "exec"> {
384
393
  /**
385
- * Executes encoded JavaScript in the context of the hook's element.
394
+ * Executes a JS command in the context of the hook's element.
386
395
  *
387
- * @param {string} encodedJS - The encoded JavaScript string to execute.
396
+ * @param encodedJS - The encoded JS command with operations to execute.
388
397
  */
389
- exec(encodedJS: string): void;
398
+ exec(encodedJS: EncodedJS): void;
390
399
  }
@@ -224,7 +224,7 @@ export default class LiveSocket {
224
224
 
225
225
  /**
226
226
  * @param {HTMLElement} el
227
- * @param {string} encodedJS
227
+ * @param {import("./js_commands").EncodedJS} encodedJS
228
228
  * @param {string | null} [eventType]
229
229
  */
230
230
  execJS(el, encodedJS, eventType = null) {
@@ -22,11 +22,12 @@ export default class LiveUploader {
22
22
  }
23
23
  }
24
24
 
25
- static getEntryDataURL(inputEl, ref, callback) {
25
+ static getEntryDataURL(inputEl, ref) {
26
26
  const file = this.activeFiles(inputEl).find(
27
27
  (file) => this.genFileRef(file) === ref,
28
28
  );
29
- callback(URL.createObjectURL(file));
29
+ if (!file) return null;
30
+ return URL.createObjectURL(file);
30
31
  }
31
32
 
32
33
  static hasUploadsInProgress(formEl) {
@@ -38,6 +38,7 @@ import {
38
38
  PHX_VIEWPORT_BOTTOM,
39
39
  MAX_CHILD_JOIN_ATTEMPTS,
40
40
  PHX_LV_PID,
41
+ PHX_NO_UNUSED_FIELD,
41
42
  PHX_PORTAL,
42
43
  PHX_TELEPORTED_REF,
43
44
  PHX_TELEPORTED_SRC,
@@ -77,90 +78,6 @@ export const prependFormDataKey = (key, prefix) => {
77
78
  return baseKey;
78
79
  };
79
80
 
80
- const serializeForm = (form, opts, onlyNames = []) => {
81
- const { submitter } = opts;
82
-
83
- // We must inject the submitter in the order that it exists in the DOM
84
- // relative to other inputs. For example, for checkbox groups, the order must be maintained.
85
- let injectedElement;
86
- if (submitter && submitter.name) {
87
- const input = document.createElement("input");
88
- input.type = "hidden";
89
- // set the form attribute if the submitter has one;
90
- // this can happen if the element is outside the actual form element
91
- const formId = submitter.getAttribute("form");
92
- if (formId) {
93
- input.setAttribute("form", formId);
94
- }
95
- input.name = submitter.name;
96
- input.value = submitter.value;
97
- submitter.parentElement.insertBefore(input, submitter);
98
- injectedElement = input;
99
- }
100
-
101
- const formData = new FormData(form);
102
- const toRemove = [];
103
-
104
- formData.forEach((val, key, _index) => {
105
- if (val instanceof File) {
106
- toRemove.push(key);
107
- }
108
- });
109
-
110
- // Cleanup after building fileData
111
- toRemove.forEach((key) => formData.delete(key));
112
-
113
- const params = new URLSearchParams();
114
-
115
- const { inputsUnused, onlyHiddenInputs } = Array.from(form.elements).reduce(
116
- (acc, input) => {
117
- const { inputsUnused, onlyHiddenInputs } = acc;
118
- const key = input.name;
119
- if (!key) {
120
- return acc;
121
- }
122
-
123
- if (inputsUnused[key] === undefined) {
124
- inputsUnused[key] = true;
125
- }
126
- if (onlyHiddenInputs[key] === undefined) {
127
- onlyHiddenInputs[key] = true;
128
- }
129
-
130
- const isUsed =
131
- DOM.private(input, PHX_HAS_FOCUSED) ||
132
- DOM.private(input, PHX_HAS_SUBMITTED);
133
- const isHidden = input.type === "hidden";
134
- inputsUnused[key] = inputsUnused[key] && !isUsed;
135
- onlyHiddenInputs[key] = onlyHiddenInputs[key] && isHidden;
136
-
137
- return acc;
138
- },
139
- { inputsUnused: {}, onlyHiddenInputs: {} },
140
- );
141
-
142
- for (const [key, val] of formData.entries()) {
143
- if (onlyNames.length === 0 || onlyNames.indexOf(key) >= 0) {
144
- const isUnused = inputsUnused[key];
145
- const hidden = onlyHiddenInputs[key];
146
- if (isUnused && !(submitter && submitter.name == key) && !hidden) {
147
- params.append(prependFormDataKey(key, "_unused_"), "");
148
- }
149
- if (typeof val === "string") {
150
- params.append(key, val);
151
- }
152
- }
153
- }
154
-
155
- // remove the injected element again
156
- // (it would be removed by the next dom patch anyway, but this is cleaner)
157
- if (submitter && injectedElement) {
158
- submitter.parentElement.removeChild(injectedElement);
159
- }
160
-
161
- return params.toString();
162
- };
163
-
164
81
  export default class View {
165
82
  static closestView(el) {
166
83
  const liveViewEl = el.closest(PHX_VIEW_SELECTOR);
@@ -1666,6 +1583,107 @@ export default class View {
1666
1583
  return meta;
1667
1584
  }
1668
1585
 
1586
+ serializeForm(form, opts, onlyNames = []) {
1587
+ const { submitter } = opts;
1588
+
1589
+ // We must inject the submitter in the order that it exists in the DOM
1590
+ // relative to other inputs. For example, for checkbox groups, the order must be maintained.
1591
+ let injectedElement;
1592
+ if (submitter && submitter.name) {
1593
+ const input = document.createElement("input");
1594
+ input.type = "hidden";
1595
+ // set the form attribute if the submitter has one;
1596
+ // this can happen if the element is outside the actual form element
1597
+ const formId = submitter.getAttribute("form");
1598
+ if (formId) {
1599
+ input.setAttribute("form", formId);
1600
+ }
1601
+ input.name = submitter.name;
1602
+ input.value = submitter.value;
1603
+ submitter.parentElement.insertBefore(input, submitter);
1604
+ injectedElement = input;
1605
+ }
1606
+
1607
+ const formData = new FormData(form);
1608
+ const toRemove = [];
1609
+
1610
+ formData.forEach((val, key, _index) => {
1611
+ if (val instanceof File) {
1612
+ toRemove.push(key);
1613
+ }
1614
+ });
1615
+
1616
+ // Cleanup after building fileData
1617
+ toRemove.forEach((key) => formData.delete(key));
1618
+
1619
+ const params = new URLSearchParams();
1620
+
1621
+ const { inputsUnused, onlyHiddenInputs } = Array.from(form.elements).reduce(
1622
+ (acc, input) => {
1623
+ const { inputsUnused, onlyHiddenInputs } = acc;
1624
+ const key = input.name;
1625
+ if (!key) {
1626
+ return acc;
1627
+ }
1628
+
1629
+ if (inputsUnused[key] === undefined) {
1630
+ inputsUnused[key] = true;
1631
+ }
1632
+ if (onlyHiddenInputs[key] === undefined) {
1633
+ onlyHiddenInputs[key] = true;
1634
+ }
1635
+
1636
+ const inputSkipUnusedField = input.hasAttribute(
1637
+ this.binding(PHX_NO_UNUSED_FIELD),
1638
+ );
1639
+
1640
+ const isUsed =
1641
+ DOM.private(input, PHX_HAS_FOCUSED) ||
1642
+ DOM.private(input, PHX_HAS_SUBMITTED) ||
1643
+ inputSkipUnusedField;
1644
+
1645
+ const isHidden = input.type === "hidden";
1646
+ inputsUnused[key] = inputsUnused[key] && !isUsed;
1647
+ onlyHiddenInputs[key] = onlyHiddenInputs[key] && isHidden;
1648
+
1649
+ return acc;
1650
+ },
1651
+ { inputsUnused: {}, onlyHiddenInputs: {} },
1652
+ );
1653
+
1654
+ const formSkipUnusedFields = form.hasAttribute(
1655
+ this.binding(PHX_NO_UNUSED_FIELD),
1656
+ );
1657
+
1658
+ for (const [key, val] of formData.entries()) {
1659
+ if (onlyNames.length === 0 || onlyNames.indexOf(key) >= 0) {
1660
+ const isUnused = inputsUnused[key];
1661
+ const hidden = onlyHiddenInputs[key];
1662
+ const skipUnusedCheck = formSkipUnusedFields;
1663
+
1664
+ if (
1665
+ !skipUnusedCheck &&
1666
+ isUnused &&
1667
+ !(submitter && submitter.name == key) &&
1668
+ !hidden
1669
+ ) {
1670
+ params.append(prependFormDataKey(key, "_unused_"), "");
1671
+ }
1672
+ if (typeof val === "string") {
1673
+ params.append(key, val);
1674
+ }
1675
+ }
1676
+ }
1677
+
1678
+ // remove the injected element again
1679
+ // (it would be removed by the next dom patch anyway, but this is cleaner)
1680
+ if (submitter && injectedElement) {
1681
+ submitter.parentElement.removeChild(injectedElement);
1682
+ }
1683
+
1684
+ return params.toString();
1685
+ }
1686
+
1669
1687
  pushEvent(type, el, targetCtx, phxEvent, meta, opts = {}, onReply) {
1670
1688
  this.pushWithReply(
1671
1689
  (maybePayload) =>
@@ -1727,9 +1745,11 @@ export default class View {
1727
1745
  serializeOpts.submitter = inputEl;
1728
1746
  }
1729
1747
  if (inputEl.getAttribute(this.binding("change"))) {
1730
- formData = serializeForm(inputEl.form, serializeOpts, [inputEl.name]);
1748
+ formData = this.serializeForm(inputEl.form, serializeOpts, [
1749
+ inputEl.name,
1750
+ ]);
1731
1751
  } else {
1732
- formData = serializeForm(inputEl.form, serializeOpts);
1752
+ formData = this.serializeForm(inputEl.form, serializeOpts);
1733
1753
  }
1734
1754
  if (
1735
1755
  DOM.isUploadInput(inputEl) &&
@@ -1906,7 +1926,7 @@ export default class View {
1906
1926
  return this.undoRefs(ref, phxEvent);
1907
1927
  }
1908
1928
  const meta = this.extractMeta(formEl, {}, opts.value);
1909
- const formData = serializeForm(formEl, { submitter });
1929
+ const formData = this.serializeForm(formEl, { submitter });
1910
1930
  this.pushWithReply(proxyRefGen, "event", {
1911
1931
  type: "form",
1912
1932
  event: phxEvent,
@@ -1924,7 +1944,7 @@ export default class View {
1924
1944
  )
1925
1945
  ) {
1926
1946
  const meta = this.extractMeta(formEl, {}, opts.value);
1927
- const formData = serializeForm(formEl, { submitter });
1947
+ const formData = this.serializeForm(formEl, { submitter });
1928
1948
  this.pushWithReply(refGenerator, "event", {
1929
1949
  type: "form",
1930
1950
  event: phxEvent,
@@ -1,4 +1,4 @@
1
- import jsCommands, { HookJSCommands } from "./js_commands";
1
+ import jsCommands, { EncodedJS, HookJSCommands } from "./js_commands";
2
2
  import DOM from "./dom";
3
3
  import LiveSocket from "./live_socket";
4
4
  import View from "./view";
@@ -395,7 +395,7 @@ export class ViewHook<E extends HTMLElement = HTMLElement>
395
395
  js(): HookJSCommands {
396
396
  return {
397
397
  ...jsCommands(this.__view().liveSocket, "hook"),
398
- exec: (encodedJS: string) => {
398
+ exec: (encodedJS: EncodedJS) => {
399
399
  this.__view().liveSocket.execJS(this.el, encodedJS, "hook");
400
400
  },
401
401
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "phoenix_live_view",
3
- "version": "1.1.30",
3
+ "version": "1.2.0-rc.1",
4
4
  "description": "The Phoenix LiveView JavaScript client.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -37,7 +37,7 @@
37
37
  "@babel/preset-env": "7.27.2",
38
38
  "@babel/preset-typescript": "^7.27.1",
39
39
  "@eslint/js": "^9.29.0",
40
- "@playwright/test": "^1.56.1",
40
+ "@playwright/test": "^1.59.1",
41
41
  "@types/jest": "^30.0.0",
42
42
  "@types/phoenix": "^1.6.6",
43
43
  "css.escape": "^1.5.1",