1
0

interpreter.js 15 KB

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