interpreter.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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. // todo call prevent default if it's the right type of event
  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) {
  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.rpc.call("user_event", {
  256. event: edit.event_name,
  257. mounted_dom_id: parseInt(realId),
  258. contents: contents,
  259. });
  260. }
  261. };
  262. this.NewEventListener(edit.event_name, edit.root, handler);
  263. break;
  264. case "SetText":
  265. this.SetText(edit.root, edit.text);
  266. break;
  267. case "SetAttribute":
  268. this.SetAttribute(edit.root, edit.field, edit.value, edit.ns);
  269. break;
  270. case "RemoveAttribute":
  271. this.RemoveAttribute(edit.root, edit.name);
  272. break;
  273. }
  274. }
  275. }
  276. export function serialize_event(event) {
  277. switch (event.type) {
  278. case "copy":
  279. case "cut":
  280. case "past": {
  281. return {};
  282. }
  283. case "compositionend":
  284. case "compositionstart":
  285. case "compositionupdate": {
  286. let { data } = event;
  287. return {
  288. data,
  289. };
  290. }
  291. case "keydown":
  292. case "keypress":
  293. case "keyup": {
  294. let {
  295. charCode,
  296. key,
  297. altKey,
  298. ctrlKey,
  299. metaKey,
  300. keyCode,
  301. shiftKey,
  302. location,
  303. repeat,
  304. which,
  305. } = event;
  306. return {
  307. char_code: charCode,
  308. key: key,
  309. alt_key: altKey,
  310. ctrl_key: ctrlKey,
  311. meta_key: metaKey,
  312. key_code: keyCode,
  313. shift_key: shiftKey,
  314. location: location,
  315. repeat: repeat,
  316. which: which,
  317. locale: "locale",
  318. };
  319. }
  320. case "focus":
  321. case "blur": {
  322. return {};
  323. }
  324. case "change": {
  325. let target = event.target;
  326. let value;
  327. if (target.type === "checkbox" || target.type === "radio") {
  328. value = target.checked ? "true" : "false";
  329. } else {
  330. value = target.value ?? target.textContent;
  331. }
  332. return {
  333. value: value,
  334. };
  335. }
  336. case "input":
  337. case "invalid":
  338. case "reset":
  339. case "submit": {
  340. let target = event.target;
  341. let value = target.value ?? target.textContent;
  342. if (target.type == "checkbox") {
  343. value = target.checked ? "true" : "false";
  344. }
  345. return {
  346. value: value,
  347. values: {},
  348. };
  349. }
  350. case "click":
  351. case "contextmenu":
  352. case "doubleclick":
  353. case "drag":
  354. case "dragend":
  355. case "dragenter":
  356. case "dragexit":
  357. case "dragleave":
  358. case "dragover":
  359. case "dragstart":
  360. case "drop":
  361. case "mousedown":
  362. case "mouseenter":
  363. case "mouseleave":
  364. case "mousemove":
  365. case "mouseout":
  366. case "mouseover":
  367. case "mouseup": {
  368. const {
  369. altKey,
  370. button,
  371. buttons,
  372. clientX,
  373. clientY,
  374. ctrlKey,
  375. metaKey,
  376. pageX,
  377. pageY,
  378. screenX,
  379. screenY,
  380. shiftKey,
  381. } = event;
  382. return {
  383. alt_key: altKey,
  384. button: button,
  385. buttons: buttons,
  386. client_x: clientX,
  387. client_y: clientY,
  388. ctrl_key: ctrlKey,
  389. meta_key: metaKey,
  390. page_x: pageX,
  391. page_y: pageY,
  392. screen_x: screenX,
  393. screen_y: screenY,
  394. shift_key: shiftKey,
  395. };
  396. }
  397. case "pointerdown":
  398. case "pointermove":
  399. case "pointerup":
  400. case "pointercancel":
  401. case "gotpointercapture":
  402. case "lostpointercapture":
  403. case "pointerenter":
  404. case "pointerleave":
  405. case "pointerover":
  406. case "pointerout": {
  407. const {
  408. altKey,
  409. button,
  410. buttons,
  411. clientX,
  412. clientY,
  413. ctrlKey,
  414. metaKey,
  415. pageX,
  416. pageY,
  417. screenX,
  418. screenY,
  419. shiftKey,
  420. pointerId,
  421. width,
  422. height,
  423. pressure,
  424. tangentialPressure,
  425. tiltX,
  426. tiltY,
  427. twist,
  428. pointerType,
  429. isPrimary,
  430. } = event;
  431. return {
  432. alt_key: altKey,
  433. button: button,
  434. buttons: buttons,
  435. client_x: clientX,
  436. client_y: clientY,
  437. ctrl_key: ctrlKey,
  438. meta_key: metaKey,
  439. page_x: pageX,
  440. page_y: pageY,
  441. screen_x: screenX,
  442. screen_y: screenY,
  443. shift_key: shiftKey,
  444. pointer_id: pointerId,
  445. width: width,
  446. height: height,
  447. pressure: pressure,
  448. tangential_pressure: tangentialPressure,
  449. tilt_x: tiltX,
  450. tilt_y: tiltY,
  451. twist: twist,
  452. pointer_type: pointerType,
  453. is_primary: isPrimary,
  454. };
  455. }
  456. case "select": {
  457. return {};
  458. }
  459. case "touchcancel":
  460. case "touchend":
  461. case "touchmove":
  462. case "touchstart": {
  463. const { altKey, ctrlKey, metaKey, shiftKey } = event;
  464. return {
  465. // changed_touches: event.changedTouches,
  466. // target_touches: event.targetTouches,
  467. // touches: event.touches,
  468. alt_key: altKey,
  469. ctrl_key: ctrlKey,
  470. meta_key: metaKey,
  471. shift_key: shiftKey,
  472. };
  473. }
  474. case "scroll": {
  475. return {};
  476. }
  477. case "wheel": {
  478. const { deltaX, deltaY, deltaZ, deltaMode } = event;
  479. return {
  480. delta_x: deltaX,
  481. delta_y: deltaY,
  482. delta_z: deltaZ,
  483. delta_mode: deltaMode,
  484. };
  485. }
  486. case "animationstart":
  487. case "animationend":
  488. case "animationiteration": {
  489. const { animationName, elapsedTime, pseudoElement } = event;
  490. return {
  491. animation_name: animationName,
  492. elapsed_time: elapsedTime,
  493. pseudo_element: pseudoElement,
  494. };
  495. }
  496. case "transitionend": {
  497. const { propertyName, elapsedTime, pseudoElement } = event;
  498. return {
  499. property_name: propertyName,
  500. elapsed_time: elapsedTime,
  501. pseudo_element: pseudoElement,
  502. };
  503. }
  504. case "abort":
  505. case "canplay":
  506. case "canplaythrough":
  507. case "durationchange":
  508. case "emptied":
  509. case "encrypted":
  510. case "ended":
  511. case "error":
  512. case "loadeddata":
  513. case "loadedmetadata":
  514. case "loadstart":
  515. case "pause":
  516. case "play":
  517. case "playing":
  518. case "progress":
  519. case "ratechange":
  520. case "seeked":
  521. case "seeking":
  522. case "stalled":
  523. case "suspend":
  524. case "timeupdate":
  525. case "volumechange":
  526. case "waiting": {
  527. return {};
  528. }
  529. case "toggle": {
  530. return {};
  531. }
  532. default: {
  533. return {};
  534. }
  535. }
  536. }
  537. const bool_attrs = {
  538. allowfullscreen: true,
  539. allowpaymentrequest: true,
  540. async: true,
  541. autofocus: true,
  542. autoplay: true,
  543. checked: true,
  544. controls: true,
  545. default: true,
  546. defer: true,
  547. disabled: true,
  548. formnovalidate: true,
  549. hidden: true,
  550. ismap: true,
  551. itemscope: true,
  552. loop: true,
  553. multiple: true,
  554. muted: true,
  555. nomodule: true,
  556. novalidate: true,
  557. open: true,
  558. playsinline: true,
  559. readonly: true,
  560. required: true,
  561. reversed: true,
  562. selected: true,
  563. truespeed: true,
  564. };