interpreter.js 17 KB

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