interpreter.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. export function main() {
  2. let root = window.document.getElementById("main");
  3. if (root != null) {
  4. window.interpreter = new Interpreter(root);
  5. window.ipc.postMessage(serializeIpcMessage("initialize"));
  6. }
  7. }
  8. export class Interpreter {
  9. constructor(root) {
  10. this.root = root;
  11. this.stack = [root];
  12. this.listeners = {};
  13. this.handlers = {};
  14. this.lastNodeWasText = false;
  15. this.nodes = [root];
  16. }
  17. top() {
  18. return this.stack[this.stack.length - 1];
  19. }
  20. pop() {
  21. return this.stack.pop();
  22. }
  23. PushRoot(root) {
  24. const node = this.nodes[root];
  25. this.stack.push(node);
  26. }
  27. AppendChildren(many) {
  28. let root = this.stack[this.stack.length - (1 + many)];
  29. let to_add = this.stack.splice(this.stack.length - many);
  30. for (let i = 0; i < many; i++) {
  31. root.appendChild(to_add[i]);
  32. }
  33. }
  34. ReplaceWith(root_id, m) {
  35. let root = this.nodes[root_id];
  36. let els = this.stack.splice(this.stack.length - m);
  37. root.replaceWith(...els);
  38. }
  39. InsertAfter(root, n) {
  40. let old = this.nodes[root];
  41. let new_nodes = this.stack.splice(this.stack.length - n);
  42. old.after(...new_nodes);
  43. }
  44. InsertBefore(root, n) {
  45. let old = this.nodes[root];
  46. let new_nodes = this.stack.splice(this.stack.length - n);
  47. old.before(...new_nodes);
  48. }
  49. Remove(root) {
  50. let node = this.nodes[root];
  51. if (node !== undefined) {
  52. node.remove();
  53. }
  54. }
  55. CreateTextNode(text, root) {
  56. // todo: make it so the types are okay
  57. const node = document.createTextNode(text);
  58. this.nodes[root] = node;
  59. this.stack.push(node);
  60. }
  61. CreateElement(tag, root) {
  62. const el = document.createElement(tag);
  63. // el.setAttribute("data-dioxus-id", `${root}`);
  64. this.nodes[root] = el;
  65. this.stack.push(el);
  66. }
  67. CreateElementNs(tag, root, ns) {
  68. let el = document.createElementNS(ns, tag);
  69. this.stack.push(el);
  70. this.nodes[root] = el;
  71. }
  72. CreatePlaceholder(root) {
  73. let el = document.createElement("pre");
  74. el.hidden = true;
  75. this.stack.push(el);
  76. this.nodes[root] = el;
  77. }
  78. NewEventListener(event_name, root, handler) {
  79. const element = this.nodes[root];
  80. element.setAttribute("data-dioxus-id", `${root}`);
  81. if (this.listeners[event_name] === undefined) {
  82. this.listeners[event_name] = 0;
  83. this.handlers[event_name] = handler;
  84. this.root.addEventListener(event_name, handler);
  85. } else {
  86. this.listeners[event_name]++;
  87. }
  88. }
  89. RemoveEventListener(root, event_name) {
  90. const element = this.nodes[root];
  91. element.removeAttribute(`data-dioxus-id`);
  92. this.listeners[event_name]--;
  93. if (this.listeners[event_name] === 0) {
  94. this.root.removeEventListener(event_name, this.handlers[event_name]);
  95. delete this.listeners[event_name];
  96. delete this.handlers[event_name];
  97. }
  98. }
  99. SetText(root, text) {
  100. this.nodes[root].textContent = text;
  101. }
  102. SetAttribute(root, field, value, ns) {
  103. const name = field;
  104. const node = this.nodes[root];
  105. if (ns === "style") {
  106. // @ts-ignore
  107. node.style[name] = value;
  108. } else if (ns !== null || ns !== undefined) {
  109. node.setAttributeNS(ns, name, value);
  110. } else {
  111. switch (name) {
  112. case "value":
  113. if (value !== node.value) {
  114. node.value = value;
  115. }
  116. break;
  117. case "checked":
  118. node.checked = value === "true";
  119. break;
  120. case "selected":
  121. node.selected = value === "true";
  122. break;
  123. case "dangerous_inner_html":
  124. node.innerHTML = value;
  125. break;
  126. default:
  127. // https://github.com/facebook/react/blob/8b88ac2592c5f555f315f9440cbb665dd1e7457a/packages/react-dom/src/shared/DOMProperty.js#L352-L364
  128. if (value === "false" && bool_attrs.hasOwnProperty(name)) {
  129. node.removeAttribute(name);
  130. } else {
  131. node.setAttribute(name, value);
  132. }
  133. }
  134. }
  135. }
  136. RemoveAttribute(root, field, ns) {
  137. const name = field;
  138. const node = this.nodes[root];
  139. if (ns !== null || ns !== undefined) {
  140. node.removeAttributeNS(ns, name);
  141. } else if (name === "value") {
  142. node.value = "";
  143. } else if (name === "checked") {
  144. node.checked = false;
  145. } else if (name === "selected") {
  146. node.selected = false;
  147. } else if (name === "dangerous_inner_html") {
  148. node.innerHTML = "";
  149. } else {
  150. node.removeAttribute(name);
  151. }
  152. }
  153. handleEdits(edits) {
  154. this.stack.push(this.root);
  155. for (let edit of edits) {
  156. this.handleEdit(edit);
  157. }
  158. }
  159. handleEdit(edit) {
  160. switch (edit.type) {
  161. case "PushRoot":
  162. this.PushRoot(edit.root);
  163. break;
  164. case "AppendChildren":
  165. this.AppendChildren(edit.many);
  166. break;
  167. case "ReplaceWith":
  168. this.ReplaceWith(edit.root, edit.m);
  169. break;
  170. case "InsertAfter":
  171. this.InsertAfter(edit.root, edit.n);
  172. break;
  173. case "InsertBefore":
  174. this.InsertBefore(edit.root, edit.n);
  175. break;
  176. case "Remove":
  177. this.Remove(edit.root);
  178. break;
  179. case "CreateTextNode":
  180. this.CreateTextNode(edit.text, edit.root);
  181. break;
  182. case "CreateElement":
  183. this.CreateElement(edit.tag, edit.root);
  184. break;
  185. case "CreateElementNs":
  186. this.CreateElementNs(edit.tag, edit.root, edit.ns);
  187. break;
  188. case "CreatePlaceholder":
  189. this.CreatePlaceholder(edit.root);
  190. break;
  191. case "RemoveEventListener":
  192. this.RemoveEventListener(edit.root, edit.event_name);
  193. break;
  194. case "NewEventListener":
  195. // this handler is only provided on desktop implementations since this
  196. // method is not used by the web implementation
  197. let handler = (event) => {
  198. let target = event.target;
  199. if (target != null) {
  200. let realId = target.getAttribute(`data-dioxus-id`);
  201. let shouldPreventDefault = target.getAttribute(
  202. `dioxus-prevent-default`
  203. );
  204. if (event.type === "click") {
  205. // todo call prevent default if it's the right type of event
  206. if (shouldPreventDefault !== `onclick`) {
  207. if (target.tagName === "A") {
  208. event.preventDefault();
  209. const href = target.getAttribute("href");
  210. if (href !== "" && href !== null && href !== undefined) {
  211. window.ipc.postMessage(
  212. serializeIpcMessage("browser_open", { href })
  213. );
  214. }
  215. }
  216. }
  217. // also prevent buttons from submitting
  218. if (target.tagName === "BUTTON") {
  219. event.preventDefault();
  220. }
  221. }
  222. // walk the tree to find the real element
  223. while (realId == null) {
  224. // we've reached the root we don't want to send an event
  225. if (target.parentElement === null) {
  226. return;
  227. }
  228. target = target.parentElement;
  229. realId = target.getAttribute(`data-dioxus-id`);
  230. }
  231. shouldPreventDefault = target.getAttribute(
  232. `dioxus-prevent-default`
  233. );
  234. let contents = serialize_event(event);
  235. if (shouldPreventDefault === `on${event.type}`) {
  236. event.preventDefault();
  237. }
  238. if (event.type === "submit") {
  239. event.preventDefault();
  240. }
  241. if (target.tagName === "FORM") {
  242. for (let x = 0; x < target.elements.length; x++) {
  243. let element = target.elements[x];
  244. let name = element.getAttribute("name");
  245. if (name != null) {
  246. if (element.getAttribute("type") === "checkbox") {
  247. // @ts-ignore
  248. contents.values[name] = element.checked ? "true" : "false";
  249. } else {
  250. // @ts-ignore
  251. contents.values[name] =
  252. element.value ?? element.textContent;
  253. }
  254. }
  255. }
  256. }
  257. if (realId === null) {
  258. return;
  259. }
  260. window.ipc.postMessage(
  261. serializeIpcMessage("user_event", {
  262. event: edit.event_name,
  263. mounted_dom_id: parseInt(realId),
  264. contents: contents,
  265. })
  266. );
  267. }
  268. };
  269. this.NewEventListener(edit.event_name, edit.root, handler);
  270. break;
  271. case "SetText":
  272. this.SetText(edit.root, edit.text);
  273. break;
  274. case "SetAttribute":
  275. this.SetAttribute(edit.root, edit.field, edit.value, edit.ns);
  276. break;
  277. case "RemoveAttribute":
  278. this.RemoveAttribute(edit.root, edit.name, edit.ns);
  279. break;
  280. }
  281. }
  282. }
  283. export function serialize_event(event) {
  284. switch (event.type) {
  285. case "copy":
  286. case "cut":
  287. case "past": {
  288. return {};
  289. }
  290. case "compositionend":
  291. case "compositionstart":
  292. case "compositionupdate": {
  293. let { data } = event;
  294. return {
  295. data,
  296. };
  297. }
  298. case "keydown":
  299. case "keypress":
  300. case "keyup": {
  301. let {
  302. charCode,
  303. key,
  304. altKey,
  305. ctrlKey,
  306. metaKey,
  307. keyCode,
  308. shiftKey,
  309. location,
  310. repeat,
  311. which,
  312. } = event;
  313. return {
  314. char_code: charCode,
  315. key: key,
  316. alt_key: altKey,
  317. ctrl_key: ctrlKey,
  318. meta_key: metaKey,
  319. key_code: keyCode,
  320. shift_key: shiftKey,
  321. location: location,
  322. repeat: repeat,
  323. which: which,
  324. locale: "locale",
  325. };
  326. }
  327. case "focus":
  328. case "blur": {
  329. return {};
  330. }
  331. case "change": {
  332. let target = event.target;
  333. let value;
  334. if (target.type === "checkbox" || target.type === "radio") {
  335. value = target.checked ? "true" : "false";
  336. } else {
  337. value = target.value ?? target.textContent;
  338. }
  339. return {
  340. value: value,
  341. values: {},
  342. };
  343. }
  344. case "input":
  345. case "invalid":
  346. case "reset":
  347. case "submit": {
  348. let target = event.target;
  349. let value = target.value ?? target.textContent;
  350. if (target.type === "checkbox") {
  351. value = target.checked ? "true" : "false";
  352. }
  353. return {
  354. value: value,
  355. values: {},
  356. };
  357. }
  358. case "click":
  359. case "contextmenu":
  360. case "doubleclick":
  361. case "drag":
  362. case "dragend":
  363. case "dragenter":
  364. case "dragexit":
  365. case "dragleave":
  366. case "dragover":
  367. case "dragstart":
  368. case "drop":
  369. case "mousedown":
  370. case "mouseenter":
  371. case "mouseleave":
  372. case "mousemove":
  373. case "mouseout":
  374. case "mouseover":
  375. case "mouseup": {
  376. const {
  377. altKey,
  378. button,
  379. buttons,
  380. clientX,
  381. clientY,
  382. ctrlKey,
  383. metaKey,
  384. pageX,
  385. pageY,
  386. screenX,
  387. screenY,
  388. shiftKey,
  389. } = event;
  390. return {
  391. alt_key: altKey,
  392. button: button,
  393. buttons: buttons,
  394. client_x: clientX,
  395. client_y: clientY,
  396. ctrl_key: ctrlKey,
  397. meta_key: metaKey,
  398. page_x: pageX,
  399. page_y: pageY,
  400. screen_x: screenX,
  401. screen_y: screenY,
  402. shift_key: shiftKey,
  403. };
  404. }
  405. case "pointerdown":
  406. case "pointermove":
  407. case "pointerup":
  408. case "pointercancel":
  409. case "gotpointercapture":
  410. case "lostpointercapture":
  411. case "pointerenter":
  412. case "pointerleave":
  413. case "pointerover":
  414. case "pointerout": {
  415. const {
  416. altKey,
  417. button,
  418. buttons,
  419. clientX,
  420. clientY,
  421. ctrlKey,
  422. metaKey,
  423. pageX,
  424. pageY,
  425. screenX,
  426. screenY,
  427. shiftKey,
  428. pointerId,
  429. width,
  430. height,
  431. pressure,
  432. tangentialPressure,
  433. tiltX,
  434. tiltY,
  435. twist,
  436. pointerType,
  437. isPrimary,
  438. } = event;
  439. return {
  440. alt_key: altKey,
  441. button: button,
  442. buttons: buttons,
  443. client_x: clientX,
  444. client_y: clientY,
  445. ctrl_key: ctrlKey,
  446. meta_key: metaKey,
  447. page_x: pageX,
  448. page_y: pageY,
  449. screen_x: screenX,
  450. screen_y: screenY,
  451. shift_key: shiftKey,
  452. pointer_id: pointerId,
  453. width: width,
  454. height: height,
  455. pressure: pressure,
  456. tangential_pressure: tangentialPressure,
  457. tilt_x: tiltX,
  458. tilt_y: tiltY,
  459. twist: twist,
  460. pointer_type: pointerType,
  461. is_primary: isPrimary,
  462. };
  463. }
  464. case "select": {
  465. return {};
  466. }
  467. case "touchcancel":
  468. case "touchend":
  469. case "touchmove":
  470. case "touchstart": {
  471. const { altKey, ctrlKey, metaKey, shiftKey } = event;
  472. return {
  473. // changed_touches: event.changedTouches,
  474. // target_touches: event.targetTouches,
  475. // touches: event.touches,
  476. alt_key: altKey,
  477. ctrl_key: ctrlKey,
  478. meta_key: metaKey,
  479. shift_key: shiftKey,
  480. };
  481. }
  482. case "scroll": {
  483. return {};
  484. }
  485. case "wheel": {
  486. const { deltaX, deltaY, deltaZ, deltaMode } = event;
  487. return {
  488. delta_x: deltaX,
  489. delta_y: deltaY,
  490. delta_z: deltaZ,
  491. delta_mode: deltaMode,
  492. };
  493. }
  494. case "animationstart":
  495. case "animationend":
  496. case "animationiteration": {
  497. const { animationName, elapsedTime, pseudoElement } = event;
  498. return {
  499. animation_name: animationName,
  500. elapsed_time: elapsedTime,
  501. pseudo_element: pseudoElement,
  502. };
  503. }
  504. case "transitionend": {
  505. const { propertyName, elapsedTime, pseudoElement } = event;
  506. return {
  507. property_name: propertyName,
  508. elapsed_time: elapsedTime,
  509. pseudo_element: pseudoElement,
  510. };
  511. }
  512. case "abort":
  513. case "canplay":
  514. case "canplaythrough":
  515. case "durationchange":
  516. case "emptied":
  517. case "encrypted":
  518. case "ended":
  519. case "error":
  520. case "loadeddata":
  521. case "loadedmetadata":
  522. case "loadstart":
  523. case "pause":
  524. case "play":
  525. case "playing":
  526. case "progress":
  527. case "ratechange":
  528. case "seeked":
  529. case "seeking":
  530. case "stalled":
  531. case "suspend":
  532. case "timeupdate":
  533. case "volumechange":
  534. case "waiting": {
  535. return {};
  536. }
  537. case "toggle": {
  538. return {};
  539. }
  540. default: {
  541. return {};
  542. }
  543. }
  544. }
  545. function serializeIpcMessage(method, params = {}) {
  546. return JSON.stringify({ method, params });
  547. }
  548. const bool_attrs = {
  549. allowfullscreen: true,
  550. allowpaymentrequest: true,
  551. async: true,
  552. autofocus: true,
  553. autoplay: true,
  554. checked: true,
  555. controls: true,
  556. default: true,
  557. defer: true,
  558. disabled: true,
  559. formnovalidate: true,
  560. hidden: true,
  561. ismap: true,
  562. itemscope: true,
  563. loop: true,
  564. multiple: true,
  565. muted: true,
  566. nomodule: true,
  567. novalidate: true,
  568. open: true,
  569. playsinline: true,
  570. readonly: true,
  571. required: true,
  572. reversed: true,
  573. selected: true,
  574. truespeed: true,
  575. };