interpreter.js 14 KB

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