interpreter.js 15 KB

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