interpreter.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. }
  92. else {
  93. this.listeners[event_name]++;
  94. }
  95. }
  96. RemoveEventListener(root, event_name) {
  97. const element = this.nodes[root];
  98. element.removeAttribute(`data-dioxus-id`);
  99. this.listeners[event_name]--;
  100. if (this.listeners[event_name] === 0) {
  101. this.root.removeEventListener(event_name, this.handlers[event_name]);
  102. delete this.listeners[event_name];
  103. delete this.handlers[event_name];
  104. }
  105. }
  106. SetText(root, text) {
  107. this.nodes[root].textContent = text;
  108. }
  109. SetAttribute(root, field, value, ns) {
  110. const name = field;
  111. const node = this.nodes[root];
  112. if (ns == "style") {
  113. // @ts-ignore
  114. node.style[name] = value;
  115. }
  116. else if (ns != null || ns != undefined) {
  117. node.setAttributeNS(ns, name, value);
  118. }
  119. else {
  120. switch (name) {
  121. case "value":
  122. if (value != node.value) {
  123. node.value = value;
  124. }
  125. break;
  126. case "checked":
  127. node.checked = value === "true";
  128. break;
  129. case "selected":
  130. node.selected = value === "true";
  131. break;
  132. case "dangerous_inner_html":
  133. node.innerHTML = value;
  134. break;
  135. default:
  136. // https://github.com/facebook/react/blob/8b88ac2592c5f555f315f9440cbb665dd1e7457a/packages/react-dom/src/shared/DOMProperty.js#L352-L364
  137. if (value == "false" && bool_attrs.hasOwnProperty(name)) {
  138. node.removeAttribute(name);
  139. }
  140. else {
  141. node.setAttribute(name, value);
  142. }
  143. }
  144. }
  145. }
  146. RemoveAttribute(root, name) {
  147. const node = this.nodes[root];
  148. node.removeAttribute(name);
  149. if (name === "value") {
  150. node.value = "";
  151. }
  152. if (name === "checked") {
  153. node.checked = false;
  154. }
  155. if (name === "selected") {
  156. node.selected = false;
  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. // this handler is only provided on desktop implementations since this
  202. // method is not used by the web implementation
  203. let handler = (event) => {
  204. let target = event.target;
  205. if (target != null) {
  206. let realId = target.getAttribute(`data-dioxus-id`);
  207. // walk the tree to find the real element
  208. while (realId == null && target.parentElement != null) {
  209. target = target.parentElement;
  210. realId = target.getAttribute(`data-dioxus-id`);
  211. }
  212. const shouldPreventDefault = target.getAttribute(`dioxus-prevent-default`);
  213. let contents = serialize_event(event);
  214. if (shouldPreventDefault === `on${event.type}`) {
  215. event.preventDefault();
  216. }
  217. if (event.type == "submit") {
  218. event.preventDefault();
  219. }
  220. if (event.type == "click") {
  221. event.preventDefault();
  222. if (shouldPreventDefault !== `onclick`) {
  223. if (target.tagName == "A") {
  224. const href = target.getAttribute("href");
  225. if (href !== "" && href !== null && href !== undefined && realId != null) {
  226. window.rpc.call("browser_open", {
  227. mounted_dom_id: parseInt(realId),
  228. href
  229. });
  230. }
  231. }
  232. }
  233. }
  234. if (target.tagName == "FORM") {
  235. let formTarget = target;
  236. for (let x = 0; x < formTarget.elements.length; x++) {
  237. let element = formTarget.elements[x];
  238. let name = element.getAttribute("name");
  239. if (name != null) {
  240. if (element.getAttribute("type") == "checkbox") {
  241. // @ts-ignore
  242. contents.values[name] = element.checked ? "true" : "false";
  243. }
  244. else {
  245. // @ts-ignore
  246. contents.values[name] = element.value ?? element.textContent;
  247. }
  248. }
  249. }
  250. }
  251. if (realId == null) {
  252. return;
  253. }
  254. window.rpc.call("user_event", {
  255. event: edit.event_name,
  256. mounted_dom_id: parseInt(realId),
  257. contents: contents,
  258. });
  259. }
  260. };
  261. this.NewEventListener(edit.event_name, edit.root, handler);
  262. break;
  263. case "SetText":
  264. this.SetText(edit.root, edit.text);
  265. break;
  266. case "SetAttribute":
  267. this.SetAttribute(edit.root, edit.field, edit.value, edit.ns);
  268. break;
  269. case "RemoveAttribute":
  270. this.RemoveAttribute(edit.root, edit.name);
  271. break;
  272. }
  273. }
  274. }
  275. function serialize_event(event) {
  276. switch (event.type) {
  277. case "copy":
  278. case "cut":
  279. case "past": {
  280. return {};
  281. }
  282. case "compositionend":
  283. case "compositionstart":
  284. case "compositionupdate": {
  285. let { data } = event;
  286. return {
  287. data,
  288. };
  289. }
  290. case "keydown":
  291. case "keypress":
  292. case "keyup": {
  293. let { charCode, key, altKey, ctrlKey, metaKey, keyCode, shiftKey, location, repeat, which, } = event;
  294. return {
  295. char_code: charCode,
  296. key: key,
  297. alt_key: altKey,
  298. ctrl_key: ctrlKey,
  299. meta_key: metaKey,
  300. key_code: keyCode,
  301. shift_key: shiftKey,
  302. location: location,
  303. repeat: repeat,
  304. which: which,
  305. locale: "locale",
  306. };
  307. }
  308. case "focus":
  309. case "blur": {
  310. return {};
  311. }
  312. case "change": {
  313. let target = event.target;
  314. let value;
  315. if (target.type === "checkbox" || target.type === "radio") {
  316. value = target.checked ? "true" : "false";
  317. }
  318. else {
  319. value = target.value ?? target.textContent;
  320. }
  321. return {
  322. value: value,
  323. };
  324. }
  325. case "input":
  326. case "invalid":
  327. case "reset":
  328. case "submit": {
  329. let target = event.target;
  330. let value = target.value ?? target.textContent;
  331. if (target.type == "checkbox") {
  332. value = target.checked ? "true" : "false";
  333. }
  334. return {
  335. value: value,
  336. values: {}
  337. };
  338. }
  339. case "click":
  340. case "contextmenu":
  341. case "doubleclick":
  342. case "drag":
  343. case "dragend":
  344. case "dragenter":
  345. case "dragexit":
  346. case "dragleave":
  347. case "dragover":
  348. case "dragstart":
  349. case "drop":
  350. case "mousedown":
  351. case "mouseenter":
  352. case "mouseleave":
  353. case "mousemove":
  354. case "mouseout":
  355. case "mouseover":
  356. case "mouseup": {
  357. const { altKey, button, buttons, clientX, clientY, ctrlKey, metaKey, pageX, pageY, screenX, screenY, shiftKey, } = event;
  358. return {
  359. alt_key: altKey,
  360. button: button,
  361. buttons: buttons,
  362. client_x: clientX,
  363. client_y: clientY,
  364. ctrl_key: ctrlKey,
  365. meta_key: metaKey,
  366. page_x: pageX,
  367. page_y: pageY,
  368. screen_x: screenX,
  369. screen_y: screenY,
  370. shift_key: shiftKey,
  371. };
  372. }
  373. case "pointerdown":
  374. case "pointermove":
  375. case "pointerup":
  376. case "pointercancel":
  377. case "gotpointercapture":
  378. case "lostpointercapture":
  379. case "pointerenter":
  380. case "pointerleave":
  381. case "pointerover":
  382. case "pointerout": {
  383. const { altKey, button, buttons, clientX, clientY, ctrlKey, metaKey, pageX, pageY, screenX, screenY, shiftKey, pointerId, width, height, pressure, tangentialPressure, tiltX, tiltY, twist, pointerType, isPrimary, } = event;
  384. return {
  385. alt_key: altKey,
  386. button: button,
  387. buttons: buttons,
  388. client_x: clientX,
  389. client_y: clientY,
  390. ctrl_key: ctrlKey,
  391. meta_key: metaKey,
  392. page_x: pageX,
  393. page_y: pageY,
  394. screen_x: screenX,
  395. screen_y: screenY,
  396. shift_key: shiftKey,
  397. pointer_id: pointerId,
  398. width: width,
  399. height: height,
  400. pressure: pressure,
  401. tangential_pressure: tangentialPressure,
  402. tilt_x: tiltX,
  403. tilt_y: tiltY,
  404. twist: twist,
  405. pointer_type: pointerType,
  406. is_primary: isPrimary,
  407. };
  408. }
  409. case "select": {
  410. return {};
  411. }
  412. case "touchcancel":
  413. case "touchend":
  414. case "touchmove":
  415. case "touchstart": {
  416. const { altKey, ctrlKey, metaKey, shiftKey, } = event;
  417. return {
  418. // changed_touches: event.changedTouches,
  419. // target_touches: event.targetTouches,
  420. // touches: event.touches,
  421. alt_key: altKey,
  422. ctrl_key: ctrlKey,
  423. meta_key: metaKey,
  424. shift_key: shiftKey,
  425. };
  426. }
  427. case "scroll": {
  428. return {};
  429. }
  430. case "wheel": {
  431. const { deltaX, deltaY, deltaZ, deltaMode, } = event;
  432. return {
  433. delta_x: deltaX,
  434. delta_y: deltaY,
  435. delta_z: deltaZ,
  436. delta_mode: deltaMode,
  437. };
  438. }
  439. case "animationstart":
  440. case "animationend":
  441. case "animationiteration": {
  442. const { animationName, elapsedTime, pseudoElement, } = event;
  443. return {
  444. animation_name: animationName,
  445. elapsed_time: elapsedTime,
  446. pseudo_element: pseudoElement,
  447. };
  448. }
  449. case "transitionend": {
  450. const { propertyName, elapsedTime, pseudoElement, } = event;
  451. return {
  452. property_name: propertyName,
  453. elapsed_time: elapsedTime,
  454. pseudo_element: pseudoElement,
  455. };
  456. }
  457. case "abort":
  458. case "canplay":
  459. case "canplaythrough":
  460. case "durationchange":
  461. case "emptied":
  462. case "encrypted":
  463. case "ended":
  464. case "error":
  465. case "loadeddata":
  466. case "loadedmetadata":
  467. case "loadstart":
  468. case "pause":
  469. case "play":
  470. case "playing":
  471. case "progress":
  472. case "ratechange":
  473. case "seeked":
  474. case "seeking":
  475. case "stalled":
  476. case "suspend":
  477. case "timeupdate":
  478. case "volumechange":
  479. case "waiting": {
  480. return {};
  481. }
  482. case "toggle": {
  483. return {};
  484. }
  485. default: {
  486. return {};
  487. }
  488. }
  489. }
  490. const bool_attrs = {
  491. allowfullscreen: true,
  492. allowpaymentrequest: true,
  493. async: true,
  494. autofocus: true,
  495. autoplay: true,
  496. checked: true,
  497. controls: true,
  498. default: true,
  499. defer: true,
  500. disabled: true,
  501. formnovalidate: true,
  502. hidden: true,
  503. ismap: true,
  504. itemscope: true,
  505. loop: true,
  506. multiple: true,
  507. muted: true,
  508. nomodule: true,
  509. novalidate: true,
  510. open: true,
  511. playsinline: true,
  512. readonly: true,
  513. required: true,
  514. reversed: true,
  515. selected: true,
  516. truespeed: true,
  517. };