cycsd / obsidian-card-note

Help you extract your thoughts more quickly in canvas
https://www.moritzjung.dev/obsidian-stats/plugins/card-note/
MIT License
30 stars 1 forks source link

gpt改好了,按键触发 #15

Closed qiekqiek closed 1 week ago

qiekqiek commented 2 weeks ago

onOpen() { const linkReferenceDescription = this.section.type === "reference" ? isHeadingBlock(this.section.block) ? " or link to heading" : " or link to block" : ""; const { contentEl } = this; let userInputText;

// 用户输入字段
const nameSetting = new import_obsidian.Setting(contentEl).setDesc(`Create file${linkReferenceDescription}`).addText((text2) => {
  var _a;
  userInputText = text2;
  text2.setValue((_a = this.userInput) != null ? _a : "");
  text2.onChange((value) => {
    this.userInput = value;
  });
}).addButton((btn) => {
  btn.setIcon("dices").setTooltip("Create random block id").setCta().onClick(() => {
    this.userInput = this.plugin.createRandomHexString();
    userInputText == null ? void 0 : userInputText.setValue(this.userInput);
  });
});

// 操作按钮
const actions = new import_obsidian.Setting(contentEl).addButton((btn) => {
  btn.setIcon("file-plus-2").setTooltip("Create file").setCta().onClick(() => {
    this.onSubmit({ type: "createFile", newName: this.userInput.trimEnd() });
    this.close();
  });
});

// 仅在引用模式下添加链接到引用按钮
if (this.section.type === "reference") {
  const section = this.section;
  actions.addButton((btn) => {
    btn.setIcon("link").setTooltip("Link to reference").setCta().onClick(() => {
      this.onSubmit({
        type: "linkToReference",
        section,
        newName: this.userInput.trimEnd()
      });
      this.close();
    });
  });
}

actions.addButton((btn) => {
  btn.setIcon("scissors").setTooltip("Cut").setCta().onClick(() => {
    this.onSubmit({ type: "cut" });
    this.close();
  });
}).addButton((btn) => {
  btn.setIcon("x").setTooltip(`Cancel`).setCta().onClick(() => {
    this.onSubmit({ type: "cancel" });
    this.close();
  });
});

if (this.errorMessage) {
  actions.setDesc(this.errorMessage);
}

// 添加键盘事件监听器
contentEl.addEventListener("keydown", (event) => {
  switch (event.key) {
    case "Enter":
      // 按下 Enter 键时创建文件
      this.onSubmit({ type: "createFile", newName: this.userInput.trimEnd() });
      this.close();
      break;

    case "Shift":
      // 按下 Shift 键时先生成块 ID,然后链接到引用(仅在引用模式下有效)
      if (this.section.type === "reference") {
        // 创建随机块 ID 并更新输入框
        this.userInput = this.plugin.createRandomHexString();
        userInputText == null ? void 0 : userInputText.setValue(this.userInput);

        // 链接到引用
        const section = this.section;
        this.onSubmit({
          type: "linkToReference",
          section,
          newName: this.userInput.trimEnd()
        });
        this.close();
      }
      break;

    case "Alt":
      // 按下 Alt 键时触发剪切操作
      this.onSubmit({ type: "cut" });
      this.close();
      break;

    case "Escape":
      // 按下 Escape 键时取消并关闭模态框
      this.onSubmit({ type: "cancel" });
      this.close();
      break;
  }
});

}