interpreter.js 17 KB

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