1
0

interpreter.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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, name) {
  137. const node = this.nodes[root];
  138. if (name === "value") {
  139. node.value = "";
  140. } else if (name === "checked") {
  141. node.checked = false;
  142. } else if (name === "selected") {
  143. node.selected = false;
  144. } else if (name === "dangerous_inner_html") {
  145. node.innerHTML = "";
  146. } else {
  147. node.removeAttribute(name);
  148. }
  149. }
  150. handleEdits(edits) {
  151. this.stack.push(this.root);
  152. for (let edit of edits) {
  153. this.handleEdit(edit);
  154. }
  155. }
  156. handleEdit(edit) {
  157. switch (edit.type) {
  158. case "PushRoot":
  159. this.PushRoot(edit.root);
  160. break;
  161. case "AppendChildren":
  162. this.AppendChildren(edit.many);
  163. break;
  164. case "ReplaceWith":
  165. this.ReplaceWith(edit.root, edit.m);
  166. break;
  167. case "InsertAfter":
  168. this.InsertAfter(edit.root, edit.n);
  169. break;
  170. case "InsertBefore":
  171. this.InsertBefore(edit.root, edit.n);
  172. break;
  173. case "Remove":
  174. this.Remove(edit.root);
  175. break;
  176. case "CreateTextNode":
  177. this.CreateTextNode(edit.text, edit.root);
  178. break;
  179. case "CreateElement":
  180. this.CreateElement(edit.tag, edit.root);
  181. break;
  182. case "CreateElementNs":
  183. this.CreateElementNs(edit.tag, edit.root, edit.ns);
  184. break;
  185. case "CreatePlaceholder":
  186. this.CreatePlaceholder(edit.root);
  187. break;
  188. case "RemoveEventListener":
  189. this.RemoveEventListener(edit.root, edit.event_name);
  190. break;
  191. case "NewEventListener":
  192. // this handler is only provided on desktop implementations since this
  193. // method is not used by the web implementation
  194. let handler = (event) => {
  195. let target = event.target;
  196. if (target != null) {
  197. let realId = target.getAttribute(`data-dioxus-id`);
  198. let shouldPreventDefault = target.getAttribute(
  199. `dioxus-prevent-default`
  200. );
  201. if (event.type === "click") {
  202. // todo call prevent default if it's the right type of event
  203. if (shouldPreventDefault !== `onclick`) {
  204. if (target.tagName === "A") {
  205. event.preventDefault();
  206. const href = target.getAttribute("href");
  207. if (href !== "" && href !== null && href !== undefined) {
  208. window.ipc.postMessage(serializeIpcMessage("browser_open", { href }))
  209. }
  210. }
  211. }
  212. // also prevent buttons from submitting
  213. if (target.tagName === "BUTTON") {
  214. event.preventDefault();
  215. }
  216. }
  217. // walk the tree to find the real element
  218. while (realId == null) {
  219. // we've reached the root we don't want to send an event
  220. if (target.parentElement === null) {
  221. return;
  222. }
  223. target = target.parentElement;
  224. realId = target.getAttribute(`data-dioxus-id`);
  225. }
  226. shouldPreventDefault = target.getAttribute(
  227. `dioxus-prevent-default`
  228. );
  229. let contents = serialize_event(event);
  230. if (shouldPreventDefault === `on${event.type}`) {
  231. event.preventDefault();
  232. }
  233. if (event.type === "submit") {
  234. event.preventDefault();
  235. }
  236. if (target.tagName === "FORM") {
  237. for (let x = 0; x < target.elements.length; x++) {
  238. let element = target.elements[x];
  239. let name = element.getAttribute("name");
  240. if (name != null) {
  241. if (element.getAttribute("type") === "checkbox") {
  242. // @ts-ignore
  243. contents.values[name] = element.checked ? "true" : "false";
  244. } else {
  245. // @ts-ignore
  246. contents.values[name] =
  247. element.value ?? element.textContent;
  248. }
  249. }
  250. }
  251. }
  252. if (realId == null) {
  253. return;
  254. }
  255. window.ipc.postMessage(serializeIpcMessage(
  256. "user_event", {
  257. event: edit.event_name,
  258. mounted_dom_id: parseInt(realId),
  259. contents: contents,
  260. }));
  261. }
  262. };
  263. this.NewEventListener(edit.event_name, edit.root, handler);
  264. break;
  265. case "SetText":
  266. this.SetText(edit.root, edit.text);
  267. break;
  268. case "SetAttribute":
  269. this.SetAttribute(edit.root, edit.field, edit.value, edit.ns);
  270. break;
  271. case "RemoveAttribute":
  272. this.RemoveAttribute(edit.root, edit.name);
  273. break;
  274. }
  275. }
  276. }
  277. export function serialize_event(event) {
  278. switch (event.type) {
  279. case "copy":
  280. case "cut":
  281. case "past": {
  282. return {};
  283. }
  284. case "compositionend":
  285. case "compositionstart":
  286. case "compositionupdate": {
  287. let { data } = event;
  288. return {
  289. data,
  290. };
  291. }
  292. case "keydown":
  293. case "keypress":
  294. case "keyup": {
  295. let {
  296. charCode,
  297. key,
  298. altKey,
  299. ctrlKey,
  300. metaKey,
  301. keyCode,
  302. shiftKey,
  303. location,
  304. repeat,
  305. which,
  306. } = event;
  307. return {
  308. char_code: charCode,
  309. key: key,
  310. alt_key: altKey,
  311. ctrl_key: ctrlKey,
  312. meta_key: metaKey,
  313. key_code: keyCode,
  314. shift_key: shiftKey,
  315. location: location,
  316. repeat: repeat,
  317. which: which,
  318. locale: "locale",
  319. };
  320. }
  321. case "focus":
  322. case "blur": {
  323. return {};
  324. }
  325. case "change": {
  326. let target = event.target;
  327. let value;
  328. if (target.type === "checkbox" || target.type === "radio") {
  329. value = target.checked ? "true" : "false";
  330. } else {
  331. value = target.value ?? target.textContent;
  332. }
  333. return {
  334. value: value,
  335. };
  336. }
  337. case "input":
  338. case "invalid":
  339. case "reset":
  340. case "submit": {
  341. let target = event.target;
  342. let value = target.value ?? target.textContent;
  343. if (target.type === "checkbox") {
  344. value = target.checked ? "true" : "false";
  345. }
  346. return {
  347. value: value,
  348. values: {},
  349. };
  350. }
  351. case "click":
  352. case "contextmenu":
  353. case "doubleclick":
  354. case "drag":
  355. case "dragend":
  356. case "dragenter":
  357. case "dragexit":
  358. case "dragleave":
  359. case "dragover":
  360. case "dragstart":
  361. case "drop":
  362. case "mousedown":
  363. case "mouseenter":
  364. case "mouseleave":
  365. case "mousemove":
  366. case "mouseout":
  367. case "mouseover":
  368. case "mouseup": {
  369. const {
  370. altKey,
  371. button,
  372. buttons,
  373. clientX,
  374. clientY,
  375. ctrlKey,
  376. metaKey,
  377. pageX,
  378. pageY,
  379. screenX,
  380. screenY,
  381. shiftKey,
  382. } = event;
  383. return {
  384. alt_key: altKey,
  385. button: button,
  386. buttons: buttons,
  387. client_x: clientX,
  388. client_y: clientY,
  389. ctrl_key: ctrlKey,
  390. meta_key: metaKey,
  391. page_x: pageX,
  392. page_y: pageY,
  393. screen_x: screenX,
  394. screen_y: screenY,
  395. shift_key: shiftKey,
  396. };
  397. }
  398. case "pointerdown":
  399. case "pointermove":
  400. case "pointerup":
  401. case "pointercancel":
  402. case "gotpointercapture":
  403. case "lostpointercapture":
  404. case "pointerenter":
  405. case "pointerleave":
  406. case "pointerover":
  407. case "pointerout": {
  408. const {
  409. altKey,
  410. button,
  411. buttons,
  412. clientX,
  413. clientY,
  414. ctrlKey,
  415. metaKey,
  416. pageX,
  417. pageY,
  418. screenX,
  419. screenY,
  420. shiftKey,
  421. pointerId,
  422. width,
  423. height,
  424. pressure,
  425. tangentialPressure,
  426. tiltX,
  427. tiltY,
  428. twist,
  429. pointerType,
  430. isPrimary,
  431. } = event;
  432. return {
  433. alt_key: altKey,
  434. button: button,
  435. buttons: buttons,
  436. client_x: clientX,
  437. client_y: clientY,
  438. ctrl_key: ctrlKey,
  439. meta_key: metaKey,
  440. page_x: pageX,
  441. page_y: pageY,
  442. screen_x: screenX,
  443. screen_y: screenY,
  444. shift_key: shiftKey,
  445. pointer_id: pointerId,
  446. width: width,
  447. height: height,
  448. pressure: pressure,
  449. tangential_pressure: tangentialPressure,
  450. tilt_x: tiltX,
  451. tilt_y: tiltY,
  452. twist: twist,
  453. pointer_type: pointerType,
  454. is_primary: isPrimary,
  455. };
  456. }
  457. case "select": {
  458. return {};
  459. }
  460. case "touchcancel":
  461. case "touchend":
  462. case "touchmove":
  463. case "touchstart": {
  464. const { altKey, ctrlKey, metaKey, shiftKey } = event;
  465. return {
  466. // changed_touches: event.changedTouches,
  467. // target_touches: event.targetTouches,
  468. // touches: event.touches,
  469. alt_key: altKey,
  470. ctrl_key: ctrlKey,
  471. meta_key: metaKey,
  472. shift_key: shiftKey,
  473. };
  474. }
  475. case "scroll": {
  476. return {};
  477. }
  478. case "wheel": {
  479. const { deltaX, deltaY, deltaZ, deltaMode } = event;
  480. return {
  481. delta_x: deltaX,
  482. delta_y: deltaY,
  483. delta_z: deltaZ,
  484. delta_mode: deltaMode,
  485. };
  486. }
  487. case "animationstart":
  488. case "animationend":
  489. case "animationiteration": {
  490. const { animationName, elapsedTime, pseudoElement } = event;
  491. return {
  492. animation_name: animationName,
  493. elapsed_time: elapsedTime,
  494. pseudo_element: pseudoElement,
  495. };
  496. }
  497. case "transitionend": {
  498. const { propertyName, elapsedTime, pseudoElement } = event;
  499. return {
  500. property_name: propertyName,
  501. elapsed_time: elapsedTime,
  502. pseudo_element: pseudoElement,
  503. };
  504. }
  505. case "abort":
  506. case "canplay":
  507. case "canplaythrough":
  508. case "durationchange":
  509. case "emptied":
  510. case "encrypted":
  511. case "ended":
  512. case "error":
  513. case "loadeddata":
  514. case "loadedmetadata":
  515. case "loadstart":
  516. case "pause":
  517. case "play":
  518. case "playing":
  519. case "progress":
  520. case "ratechange":
  521. case "seeked":
  522. case "seeking":
  523. case "stalled":
  524. case "suspend":
  525. case "timeupdate":
  526. case "volumechange":
  527. case "waiting": {
  528. return {};
  529. }
  530. case "toggle": {
  531. return {};
  532. }
  533. default: {
  534. return {};
  535. }
  536. }
  537. }
  538. function serializeIpcMessage(method, params = {}) {
  539. return JSON.stringify({ method, params });
  540. }
  541. const bool_attrs = {
  542. allowfullscreen: true,
  543. allowpaymentrequest: true,
  544. async: true,
  545. autofocus: true,
  546. autoplay: true,
  547. checked: true,
  548. controls: true,
  549. default: true,
  550. defer: true,
  551. disabled: true,
  552. formnovalidate: true,
  553. hidden: true,
  554. ismap: true,
  555. itemscope: true,
  556. loop: true,
  557. multiple: true,
  558. muted: true,
  559. nomodule: true,
  560. novalidate: true,
  561. open: true,
  562. playsinline: true,
  563. readonly: true,
  564. required: true,
  565. reversed: true,
  566. selected: true,
  567. truespeed: true,
  568. };