bindings.rs 18 KB

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