summaryrefslogtreecommitdiff
path: root/SI/.obsidian/plugins/obsidian-advanced-new-file/main.js
blob: 9efe6a65f158b0369644cd1fbfff454082d3ce49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
Obsidian Advanced New File
https://github.com/vanadium23/obsidian-advanced-new-file
*/

var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
var __export = (target, all) => {
  __markAsModule(target);
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __reExport = (target, module2, desc) => {
  if (module2 && typeof module2 === "object" || typeof module2 === "function") {
    for (let key of __getOwnPropNames(module2))
      if (!__hasOwnProp.call(target, key) && key !== "default")
        __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
  }
  return target;
};
var __toModule = (module2) => {
  return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2);
};
var __async = (__this, __arguments, generator) => {
  return new Promise((resolve, reject) => {
    var fulfilled = (value) => {
      try {
        step(generator.next(value));
      } catch (e) {
        reject(e);
      }
    };
    var rejected = (value) => {
      try {
        step(generator.throw(value));
      } catch (e) {
        reject(e);
      }
    };
    var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
    step((generator = generator.apply(__this, __arguments)).next());
  });
};

// src/main.ts
__export(exports, {
  default: () => AdvancedNewFilePlugin
});
var import_obsidian4 = __toModule(require("obsidian"));

// src/ChooseFolderModal.ts
var import_obsidian3 = __toModule(require("obsidian"));

// src/CreateNoteModal.ts
var import_obsidian2 = __toModule(require("obsidian"));

// src/enums.ts
var NewFileLocation;
(function(NewFileLocation2) {
  NewFileLocation2["CurrentPane"] = "current-pane";
  NewFileLocation2["NewPane"] = "new-pane";
  NewFileLocation2["NewTab"] = "new-tab";
})(NewFileLocation || (NewFileLocation = {}));

// src/utils.ts
var import_obsidian = __toModule(require("obsidian"));
var path = {
  parse(pathString) {
    const regex = /(?<dir>([^/\\]+[/\\])*)(?<name>[^/\\]*$)/;
    const match = String(pathString).match(regex);
    const { dir, name } = match && match.groups;
    return { dir, name: name || "Untitled" };
  },
  join(...strings) {
    const parts = strings.map((s) => String(s).trim()).filter((s) => s != null);
    return (0, import_obsidian.normalizePath)(parts.join("/"));
  }
};

// src/CreateNoteModal.ts
var CreateNoteModal = class extends import_obsidian2.Modal {
  constructor(app, mode) {
    super(app);
    this.mode = mode;
    this.inputEl = document.createElement("input");
    this.inputEl.type = "text";
    this.inputEl.placeholder = "Type filename for new note";
    this.inputEl.className = "prompt-input";
    const instructions2 = [
      {
        command: "\u21B5",
        purpose: "to create note (default: Untitled)"
      },
      {
        command: "esc",
        purpose: "to dismiss creation"
      }
    ];
    this.instructionsEl = document.createElement("div");
    this.instructionsEl.addClass("prompt-instructions");
    const children = instructions2.map((x) => {
      const child = document.createElement("div");
      child.addClass("prompt-instruction");
      const command = document.createElement("span");
      command.addClass("prompt-instruction-command");
      command.innerText = x.command;
      child.appendChild(command);
      const purpose = document.createElement("span");
      purpose.innerText = x.purpose;
      child.appendChild(purpose);
      return child;
    });
    for (const child of children) {
      this.instructionsEl.appendChild(child);
    }
    this.modalEl.className = "prompt";
    this.modalEl.innerHTML = "";
    this.modalEl.appendChild(this.inputEl);
    this.modalEl.appendChild(this.instructionsEl);
    this.inputListener = this.listenInput.bind(this);
  }
  setFolder(folder, newDirectoryPath) {
    this.folder = folder;
    this.newDirectoryPath = newDirectoryPath;
  }
  listenInput(evt) {
    if (evt.key === "Enter") {
      evt.preventDefault();
      this.createNewNote(this.inputEl.value);
      this.close();
    }
  }
  onOpen() {
    this.inputEl.focus();
    this.inputEl.addEventListener("keydown", this.inputListener);
  }
  onClose() {
    this.inputEl.removeEventListener("keydown", this.inputListener);
  }
  createDirectory(dir) {
    return __async(this, null, function* () {
      const { vault } = this.app;
      const { adapter } = vault;
      const root = vault.getRoot().path;
      const directoryPath = path.join(this.folder.path, dir);
      const directoryExists = yield adapter.exists(directoryPath);
      if (!import_obsidian2.Platform.isIosApp) {
        if (!directoryExists) {
          return adapter.mkdir((0, import_obsidian2.normalizePath)(directoryPath));
        }
      }
      const subPaths = (0, import_obsidian2.normalizePath)(directoryPath).split("/").filter((part) => part.trim() !== "").map((_, index, arr) => arr.slice(0, index + 1).join("/"));
      for (const subPath of subPaths) {
        const directoryExists2 = yield adapter.exists(path.join(root, subPath));
        if (!directoryExists2) {
          yield adapter.mkdir(path.join(root, subPath));
        }
      }
    });
  }
  createNewNote(input) {
    return __async(this, null, function* () {
      const { vault } = this.app;
      const { adapter } = vault;
      const prependDirInput = path.join(this.newDirectoryPath, input);
      const { dir, name } = path.parse(prependDirInput);
      const directoryPath = path.join(this.folder.path, dir);
      const filePath = path.join(directoryPath, `${name}.md`);
      try {
        const fileExists = yield adapter.exists(filePath);
        if (fileExists) {
          throw new Error(`${filePath} already exists`);
        }
        if (dir !== "") {
          yield this.createDirectory(dir);
        }
        const File = yield vault.create(filePath, "");
        let leaf = this.app.workspace.getLeaf(false);
        if (this.mode === NewFileLocation.NewPane) {
          leaf = this.app.workspace.splitLeafOrActive();
        } else if (this.mode === NewFileLocation.NewTab) {
          leaf = this.app.workspace.getLeaf(true);
        } else if (!leaf) {
          leaf = this.app.workspace.getLeaf(true);
        }
        yield leaf.openFile(File);
      } catch (error) {
        new import_obsidian2.Notice(error.toString());
      }
    });
  }
};

// src/ChooseFolderModal.ts
var EMPTY_TEXT = "No folder found. Press esc to dismiss.";
var PLACEHOLDER_TEXT = "Type folder name to fuzzy find.";
var instructions = [
  { command: "\u2191\u2193", purpose: "to navigate" },
  { command: "Tab \u21B9", purpose: "to autocomplete folder" },
  { command: "\u21B5", purpose: "to choose folder" },
  { command: "esc", purpose: "to dismiss" }
];
var ChooseFolderModal = class extends import_obsidian3.FuzzySuggestModal {
  constructor(app, mode) {
    super(app);
    this.mode = mode;
    this.init();
  }
  init() {
    const folders = new Set();
    const sortedFolders = [];
    let leaf = this.app.workspace.getLeaf(false);
    if (leaf && leaf.view instanceof import_obsidian3.MarkdownView && leaf.view.file instanceof import_obsidian3.TFile && leaf.view.file.parent instanceof import_obsidian3.TFolder) {
      folders.add(leaf.view.file.parent);
      sortedFolders.push(leaf.view.file.parent);
    }
    import_obsidian3.Vault.recurseChildren(this.app.vault.getRoot(), (file) => {
      if (file instanceof import_obsidian3.TFolder && !folders.has(file)) {
        folders.add(file);
        sortedFolders.push(file);
      }
    });
    this.folders = sortedFolders;
    this.emptyStateText = EMPTY_TEXT;
    this.setPlaceholder(PLACEHOLDER_TEXT);
    this.setInstructions(instructions);
    this.initChooseFolderItem();
    this.createNoteModal = new CreateNoteModal(this.app, this.mode);
    this.inputListener = this.listenInput.bind(this);
  }
  getItems() {
    return this.folders;
  }
  getItemText(item) {
    this.noSuggestion = false;
    return item.path;
  }
  onNoSuggestion() {
    this.noSuggestion = true;
    this.newDirectoryPath = this.inputEl.value;
    this.resultContainerEl.childNodes.forEach((c) => c.parentNode.removeChild(c));
    this.chooseFolder.innerText = this.inputEl.value;
    this.itemInstructionMessage(this.chooseFolder, "Press \u21B5 or append / to create folder.");
    this.resultContainerEl.appendChild(this.chooseFolder);
    this.resultContainerEl.appendChild(this.suggestionEmpty);
  }
  shouldCreateFolder(evt) {
    if (this.newDirectoryPath.endsWith("/")) {
      return true;
    }
    if (evt instanceof KeyboardEvent && evt.key == "Enter") {
      return true;
    }
    return false;
  }
  findCurrentSelect() {
    return document.querySelector(".suggestion-item.is-selected");
  }
  listenInput(evt) {
    var _a;
    if (evt.key == "Tab") {
      this.inputEl.value = (_a = this.findCurrentSelect()) == null ? void 0 : _a.innerText;
      evt.preventDefault();
    }
  }
  onOpen() {
    super.onOpen();
    this.inputEl.addEventListener("keydown", this.inputListener);
  }
  onClose() {
    this.inputEl.removeEventListener("keydown", this.inputListener);
    super.onClose();
  }
  onChooseItem(item, evt) {
    if (this.noSuggestion) {
      if (!this.shouldCreateFolder(evt)) {
        return;
      }
      this.createNoteModal.setFolder(this.app.vault.getRoot(), this.newDirectoryPath);
    } else {
      this.createNoteModal.setFolder(item, "");
    }
    this.createNoteModal.open();
  }
  initChooseFolderItem() {
    this.chooseFolder = document.createElement("div");
    this.chooseFolder.addClasses(["suggestion-item", "is-selected"]);
    this.suggestionEmpty = document.createElement("div");
    this.suggestionEmpty.addClass("suggestion-empty");
    this.suggestionEmpty.innerText = EMPTY_TEXT;
  }
  itemInstructionMessage(resultEl, message) {
    const el = document.createElement("kbd");
    el.addClass("suggestion-hotkey");
    el.innerText = message;
    resultEl.appendChild(el);
  }
};

// src/main.ts
var AdvancedNewFilePlugin = class extends import_obsidian4.Plugin {
  onload() {
    return __async(this, null, function* () {
      console.log("loading plugin");
      this.addCommand({
        id: "advanced-new-file",
        name: "Create note in the current pane",
        callback: () => {
          new ChooseFolderModal(this.app, NewFileLocation.CurrentPane).open();
        }
      });
      this.addCommand({
        id: "advanced-new-file-new-pane",
        name: "Create note in a new pane",
        callback: () => {
          new ChooseFolderModal(this.app, NewFileLocation.NewPane).open();
        }
      });
      this.addCommand({
        id: "advanced-new-file-new-tab",
        name: "Create note in a new tab",
        callback: () => {
          new ChooseFolderModal(this.app, NewFileLocation.NewTab).open();
        }
      });
    });
  }
  onunload() {
    console.log("unloading plugin");
  }
};