obsidian#ClipboardManager TypeScript Examples

The following examples show how to use obsidian#ClipboardManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: drag-patch.ts    From alx-folder-note with MIT License 5 votes vote down vote up
PatchDragManager = (plugin: ALxFolderNote) => {
  const { getFolderNote } = plugin.CoreApi;

  const patchClipboardManager = (): boolean => {
    const view = getMarkdownView();
    if (!view) return false;
    const editMode = view.editMode ?? view.sourceMode;

    if (!editMode)
      throw new Error("Failed to patch clipboard manager: no edit view found");

    plugin.register(
      around(
        editMode.clipboardManager.constructor.prototype as ClipboardManager,
        {
          handleDragOver: (next) =>
            function (this: ClipboardManager, evt, ...args) {
              const { draggable } = this.app.dragManager;
              if (
                draggable &&
                !(Platform.isMacOS ? evt.shiftKey : evt.altKey) &&
                draggable.file instanceof TFolder &&
                getFolderNote(draggable.file)
              ) {
                // evt.preventDefault();
                VD(evt, "link");
                this.app.dragManager.setAction(
                  i18next.t("interface.drag-and-drop.insert-link-here"),
                );
              } else {
                next.call(this, evt, ...args);
              }
            },
          handleDrop: (next) =>
            function (this: ClipboardManager, evt, ...args) {
              const fallback = () => next.call(this, evt, ...args);
              const { draggable } = plugin.app.dragManager;
              let note;
              if (
                draggable?.type === "folder" &&
                draggable.file instanceof TFolder &&
                (note = getFolderNote(draggable.file))
              ) {
                draggable.file = note;
                draggable.type = "file";
              }
              return fallback();
            },
        },
      ),
    );
    console.log("alx-folder-note: clipboard manager patched");
    return true;
  };

  plugin.app.workspace.onLayoutReady(() => {
    if (!patchClipboardManager()) {
      const evt = app.workspace.on("layout-change", () => {
        patchClipboardManager() && app.workspace.offref(evt);
      });
      plugin.registerEvent(evt);
    }
  });

  plugin.register(
    around(app.dragManager.constructor.prototype as DragManager, {
      dragFolder: (next) =>
        function (this: DragManager, evt, folder, source, ...args) {
          let note;
          if ((note = getFolderNote(folder))) {
            const url = app.getObsidianUrl(note);
            evt.dataTransfer!.setData("text/plain", url);
            evt.dataTransfer!.setData("text/uri-list", url);
          }
          return next.call(this, evt, folder, source, ...args);
        },
    }),
  );
}