Editor.res.mjs

  1// Generated by ReScript, PLEASE EDIT WITH CARE
  2
  3import * as Log from "../utils/Log.res.mjs";
  4import * as State from "../State.res.mjs";
  5import * as NanoID from "../ffi/NanoID.res.mjs";
  6import * as Preact from "../ffi/Preact.res.mjs";
  7import * as Tiptap from "../ffi/Tiptap.res.mjs";
  8import * as PreactDOM from "../ffi/PreactDOM.res.mjs";
  9import * as Core from "@tiptap/core";
 10import * as Core__Option from "@rescript/core/lib/es6/src/Core__Option.res.mjs";
 11import * as React from "@tiptap/react";
 12import * as WaypointSearch from "./WaypointSearch.res.mjs";
 13import * as Signals from "@preact/signals";
 14import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js";
 15import * as JsxRuntime from "preact/jsx-runtime";
 16
 17let FloatingUI = {};
 18
 19let Instance = {};
 20
 21let onSearchSelectCache = new Map();
 22
 23function Editor(props) {
 24  let hidden = props.hidden;
 25  let disabled = props.disabled;
 26  let doc = props.doc;
 27  let classes = [
 28    "w-full",
 29    "px-3",
 30    "pb-[50vh]",
 31    "md:pb-14"
 32  ];
 33  let instance = State.Editor.cache.get(doc);
 34  let editor;
 35  if (instance !== undefined) {
 36    editor = Primitive_option.valFromOption(instance);
 37  } else {
 38    let ydoc = State.getOrCreateYjsDoc(doc);
 39    let editorRef = {
 40      contents: undefined
 41    };
 42    let instance$1 = new Core.Editor({
 43      editorProps: {
 44        attributes: {
 45          class: classes.join(" ")
 46        },
 47        scrollMargin: {
 48          bottom: 48
 49        }
 50      },
 51      extensions: Tiptap.extensions(ydoc, doc),
 52      onUpdate: _event => {
 53        let ed = editorRef.contents;
 54        if (ed === undefined) {
 55          return;
 56        }
 57        let ed$1 = Primitive_option.valFromOption(ed);
 58        State.WaypointUpdater.trigger(doc, ed$1);
 59        console.log(ed$1.getJSON());
 60      }
 61    });
 62    editorRef.contents = Primitive_option.some(instance$1);
 63    instance$1.commands.setMeta("id", doc);
 64    State.Editor.cache.set(doc, instance$1);
 65    State.WaypointUpdater.trigger(doc, instance$1);
 66    editor = instance$1;
 67  }
 68  Signals.effect(() => {
 69    if (disabled === undefined) {
 70      return;
 71    }
 72    let sig = Primitive_option.valFromOption(disabled);
 73    editor.setEditable(!sig.value);
 74    if (sig.value) {
 75      Log.debug("editor disabled: blur", undefined);
 76      editor.commands.blur();
 77    } else {
 78      Log.debug("editor enabled: focus", undefined);
 79      editor.commands.focus();
 80    }
 81  });
 82  let clsName = hidden !== undefined ? Preact.cls("w-full h-full px-2 mb-6", {
 83      hidden: Primitive_option.valFromOption(hidden).value
 84    }) : "w-full h-full px-2";
 85  let sig = onSearchSelectCache.get(doc);
 86  let onSearchSelect;
 87  if (sig !== undefined) {
 88    onSearchSelect = Primitive_option.valFromOption(sig);
 89  } else {
 90    let searchSelect = Signals.signal(undefined);
 91    onSearchSelectCache.set(doc, searchSelect);
 92    onSearchSelect = searchSelect;
 93  }
 94  let showSearch = Signals.computed(() => Core__Option.isSome(onSearchSelect.value));
 95  Signals.effect(() => {
 96    let match = onSearchSelect.value;
 97    if (match !== undefined) {
 98      return;
 99    }
100    let handleWaypointSearch = event => {
101      let match = onSearchSelect.value;
102      if (match !== undefined) {
103        return;
104      }
105      let selectFn = result => {
106        let range = event.range;
107        if (range !== undefined) {
108          let attrs_id = NanoID.make();
109          let attrs_point = [
110            result.lon,
111            result.lat
112          ];
113          let attrs_label = result.name;
114          let attrs_gid = result.gid.toString();
115          let attrs_country = result.country;
116          let attrs_cons = [];
117          let attrs = {
118            id: attrs_id,
119            point: attrs_point,
120            from: undefined,
121            label: attrs_label,
122            gid: attrs_gid,
123            country: attrs_country,
124            cons: attrs_cons,
125            nonroutable: false,
126            error: undefined
127          };
128          let content = event.content(attrs);
129          let rangeObj = {
130            from: range.from,
131            to: range.to
132          };
133          editor.commands.insertContentAt(rangeObj, content);
134        }
135        onSearchSelect.value = undefined;
136      };
137      onSearchSelect.value = selectFn;
138    };
139    let documentElement = document;
140    let cleanup = PreactDOM.addEventListener(documentElement, "waypointsearch", handleWaypointSearch);
141    return () => {
142      Log.debug("Document listener cleanup - removing event listener", undefined);
143      cleanup();
144    };
145  });
146  let elem = State.content.value;
147  let tmp;
148  if (elem !== undefined) {
149    tmp = elem;
150  } else if (showSearch.value) {
151    let fn = onSearchSelect.value;
152    let onSelect = fn !== undefined ? fn : _result => {};
153    let onClose = () => {
154      onSearchSelect.value = undefined;
155    };
156    tmp = JsxRuntime.jsx(WaypointSearch.make, {
157      onSelect: onSelect,
158      onClose: onClose
159    });
160  } else {
161    tmp = JsxRuntime.jsx(React.Tiptap, {
162      instance: editor,
163      id: doc,
164      className: props.className,
165      children: props.children
166    });
167  }
168  return JsxRuntime.jsx("div", {
169    children: tmp,
170    className: clsName,
171    onClick: _evt => {
172      editor.commands.focus();
173    }
174  });
175}
176
177let Content = {};
178
179let BubbleMenu = {};
180
181let FloatingMenu = {};
182
183let make = Editor;
184
185export {
186  FloatingUI,
187  Instance,
188  onSearchSelectCache,
189  make,
190  Content,
191  BubbleMenu,
192  FloatingMenu,
193}
194/* onSearchSelectCache Not a pure module */