Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] QuickInsert Text of sub-note #174

Closed
ynck-codes opened this issue Oct 9, 2022 · 9 comments
Closed

[Feature] QuickInsert Text of sub-note #174

ynck-codes opened this issue Oct 9, 2022 · 9 comments
Labels
enhancement New feature or request

Comments

@ynck-codes
Copy link

First of all, thank you very much for your fast bug-support🙏🏼

I wonder if there is a way to modify the QuickInsert button behavior. I would rather have the text of the sub-note inserted into the main note instead of a link to the sub-note. I know that I can click onto the link and insert the sub-note as a "linked Note" but that is plenty of work to do when having loads of sub-notes.

Perhaps creating a different button to really quick insert text of sub-note to main-note would do the thing.
It would also be nice if the text updates in main-note when changing the corresponding sub-note.

I may overlook something but is there any chance to realise this kind of behavior?

@ynck-codes ynck-codes added the enhancement New feature or request label Oct 9, 2022
@windingwind
Copy link
Owner

If you mean this button:
image

Then absolutely YES!

The link content and style are controlled by the template called "QuickInsert". Please update to the latest and replace it with the text below:

${await new Promise(async (r) => {
  let newLines = [];
  const convertResult = await Zotero.Knowledge4Zotero.NoteUtils.convertNoteLines(
    subNoteItem,
    [],
    true
  );
  const subNoteLines = convertResult.lines;
  // Prevent note to be too long
  if (subNoteLines.join("\n").length > 100000) {
    Zotero.Knowledge4Zotero.ZoteroViews.showProgressWindow(
      "Better Notes",
      "The linked note is too long. Import ignored."
    );
    return;
  }
  const templateText =
    await Zotero.Knowledge4Zotero.TemplateController.renderTemplateAsync(
      "[QuickImport]",
      "subNoteLines, subNoteItem, noteItem",
      [subNoteLines, subNoteItem, noteItem]
    );
  newLines.push(templateText);
  const newLineString = newLines.join("\n");
  const notifyFlag = Zotero.Promise.defer();
  const notifierName = "insertLinkWait";
  Zotero.Knowledge4Zotero.events.addNotifyListener(
    notifierName,
    (event, type, ids, extraData) => {
      if (
        event === "modify" &&
        type === "item" &&
        ids.includes(noteItem.id)
      ) {
        notifyFlag.resolve();
        Zotero.Knowledge4Zotero.events.removeNotifyListener(notifierName);
      }
    }
  );
  await Zotero.Knowledge4Zotero.NoteUtils.modifyLineInNote(
    noteItem,
    (oldLine) => {
      Zotero.debug(oldLine);
      const params = Zotero.Knowledge4Zotero.NoteParse.parseParamsFromLink(link);
      const newLink = !params.ignore
        ? link + (link.includes("?") ? "&ignore=1" : "?ignore=1")
        : link;
      const linkIndex = Zotero.Knowledge4Zotero.NoteParse.parseLinkIndexInText(oldLine);
      Zotero.debug(linkIndex);
      return `${oldLine.slice(0, linkIndex[0])}${newLink}${oldLine.slice(
        linkIndex[1]
      )}\n${newLineString}`;
    },
    lineIndex,
    true
  );
  // wait the first modify finish
  await notifyFlag.promise;
  let hasAttachemnts = false;
  for (const _n of [subNoteItem, ...convertResult.subNotes]) {
    if (Zotero.Items.get(_n.getAttachments()).length) {
      hasAttachemnts = true;
      break;
    }
  }
  if (hasAttachemnts) {
    await Zotero.DB.executeTransaction(async () => {
      await Zotero.Notes.copyEmbeddedImages(subNoteItem, noteItem);
      for (const subNote of convertResult.subNotes) {
        await Zotero.Notes.copyEmbeddedImages(subNote, noteItem);
      }
    });
    await Zotero.Knowledge4Zotero.NoteUtils.scrollWithRefresh(
      Zotero.Knowledge4Zotero.NoteUtils.currentLine[noteItem.id]
    );
  }
})}

It is part of the plugin's code for embedding the linked note content. Be aware the APIs used in this template may change in future releases.

@ynck-codes
Copy link
Author

Unfortunately the following error appears when clicking the button with changed [QuickInsert] template:

image

At first glance I cannot find any missing curly braces.

@windingwind
Copy link
Owner

windingwind commented Oct 9, 2022

Please check for updates on the plugin page and update plugin to 0.7.8.

@ynck-codes
Copy link
Author

Awesome, works like a charme. Thank you very much.

@ynck-codes
Copy link
Author

the provided code snippet does not work anymore.
Could you please help me out?

@windingwind
Copy link
Owner

The API Zotero.Knowledge4Zotero.events is renamed to Zotero.Knowledge4Zotero.ZoteroEvents. Replace it with the new API:

${await new Promise(async (r) => {
  let newLines = [];
  const convertResult = await Zotero.Knowledge4Zotero.NoteUtils.convertNoteLines(
    subNoteItem,
    [],
    true
  );
  const subNoteLines = convertResult.lines;
  // Prevent note to be too long
  if (subNoteLines.join("\n").length > 100000) {
    Zotero.Knowledge4Zotero.ZoteroViews.showProgressWindow(
      "Better Notes",
      "The linked note is too long. Import ignored."
    );
    return;
  }
  const templateText =
    await Zotero.Knowledge4Zotero.TemplateController.renderTemplateAsync(
      "[QuickImport]",
      "subNoteLines, subNoteItem, noteItem",
      [subNoteLines, subNoteItem, noteItem]
    );
  newLines.push(templateText);
  const newLineString = newLines.join("\n");
  const notifyFlag = Zotero.Promise.defer();
  const notifierName = "insertLinkWait";
  Zotero.Knowledge4Zotero.ZoteroEvents.addNotifyListener(
    notifierName,
    (event, type, ids, extraData) => {
      if (
        event === "modify" &&
        type === "item" &&
        ids.includes(noteItem.id)
      ) {
        notifyFlag.resolve();
        Zotero.Knowledge4Zotero.ZoteroEvents.removeNotifyListener(notifierName);
      }
    }
  );
  await Zotero.Knowledge4Zotero.NoteUtils.modifyLineInNote(
    noteItem,
    (oldLine) => {
      Zotero.debug(oldLine);
      const params = Zotero.Knowledge4Zotero.NoteParse.parseParamsFromLink(link);
      const newLink = !params.ignore
        ? link + (link.includes("?") ? "&ignore=1" : "?ignore=1")
        : link;
      const linkIndex = Zotero.Knowledge4Zotero.NoteParse.parseLinkIndexInText(oldLine);
      Zotero.debug(linkIndex);
      return `${oldLine.slice(0, linkIndex[0])}${newLink}${oldLine.slice(
        linkIndex[1]
      )}\n${newLineString}`;
    },
    lineIndex,
    true
  );
  // wait the first modify finish
  await notifyFlag.promise;
  let hasAttachemnts = false;
  for (const _n of [subNoteItem, ...convertResult.subNotes]) {
    if (Zotero.Items.get(_n.getAttachments()).length) {
      hasAttachemnts = true;
      break;
    }
  }
  if (hasAttachemnts) {
    await Zotero.DB.executeTransaction(async () => {
      await Zotero.Notes.copyEmbeddedImages(subNoteItem, noteItem);
      for (const subNote of convertResult.subNotes) {
        await Zotero.Notes.copyEmbeddedImages(subNote, noteItem);
      }
    });
    await Zotero.Knowledge4Zotero.NoteUtils.scrollWithRefresh(
      Zotero.Knowledge4Zotero.NoteUtils.currentLine[noteItem.id]
    );
  }
})}

@SYH0323
Copy link

SYH0323 commented Feb 16, 2023

If you mean this button: image

Then absolutely YES!

The link content and style are controlled by the template called "QuickInsert". Please update to the latest and replace it with the text below:

${await new Promise(async (r) => {
  let newLines = [];
  const convertResult = await Zotero.Knowledge4Zotero.NoteUtils.convertNoteLines(
    subNoteItem,
    [],
    true
  );
  const subNoteLines = convertResult.lines;
  // Prevent note to be too long
  if (subNoteLines.join("\n").length > 100000) {
    Zotero.Knowledge4Zotero.ZoteroViews.showProgressWindow(
      "Better Notes",
      "The linked note is too long. Import ignored."
    );
    return;
  }
  const templateText =
    await Zotero.Knowledge4Zotero.TemplateController.renderTemplateAsync(
      "[QuickImport]",
      "subNoteLines, subNoteItem, noteItem",
      [subNoteLines, subNoteItem, noteItem]
    );
  newLines.push(templateText);
  const newLineString = newLines.join("\n");
  const notifyFlag = Zotero.Promise.defer();
  const notifierName = "insertLinkWait";
  Zotero.Knowledge4Zotero.events.addNotifyListener(
    notifierName,
    (event, type, ids, extraData) => {
      if (
        event === "modify" &&
        type === "item" &&
        ids.includes(noteItem.id)
      ) {
        notifyFlag.resolve();
        Zotero.Knowledge4Zotero.events.removeNotifyListener(notifierName);
      }
    }
  );
  await Zotero.Knowledge4Zotero.NoteUtils.modifyLineInNote(
    noteItem,
    (oldLine) => {
      Zotero.debug(oldLine);
      const params = Zotero.Knowledge4Zotero.NoteParse.parseParamsFromLink(link);
      const newLink = !params.ignore
        ? link + (link.includes("?") ? "&ignore=1" : "?ignore=1")
        : link;
      const linkIndex = Zotero.Knowledge4Zotero.NoteParse.parseLinkIndexInText(oldLine);
      Zotero.debug(linkIndex);
      return `${oldLine.slice(0, linkIndex[0])}${newLink}${oldLine.slice(
        linkIndex[1]
      )}\n${newLineString}`;
    },
    lineIndex,
    true
  );
  // wait the first modify finish
  await notifyFlag.promise;
  let hasAttachemnts = false;
  for (const _n of [subNoteItem, ...convertResult.subNotes]) {
    if (Zotero.Items.get(_n.getAttachments()).length) {
      hasAttachemnts = true;
      break;
    }
  }
  if (hasAttachemnts) {
    await Zotero.DB.executeTransaction(async () => {
      await Zotero.Notes.copyEmbeddedImages(subNoteItem, noteItem);
      for (const subNote of convertResult.subNotes) {
        await Zotero.Notes.copyEmbeddedImages(subNote, noteItem);
      }
    });
    await Zotero.Knowledge4Zotero.NoteUtils.scrollWithRefresh(
      Zotero.Knowledge4Zotero.NoteUtils.currentLine[noteItem.id]
    );
  }
})}

It is part of the plugin's code for embedding the linked note content. Be aware the APIs used in this template may change in future releases.

大佬,请问这段粘贴到哪里呢?我直接在【编辑】→【模板】那里改quickinsert的代码不行。

@windingwind
Copy link
Owner

Notifier callback registration in Zotero.Knowledge4Zotero.ZoteroEvents is moved to Zotero.Knowledge4Zotero.ZoteroNotifies.

As I stated before, this template uses on inner APIs and you should not rely on it.

${await new Promise(async (r) => {
  let newLines = [];
  const convertResult = await Zotero.Knowledge4Zotero.NoteUtils.convertNoteLines(
    subNoteItem,
    [],
    true
  );
  const subNoteLines = convertResult.lines;
  // Prevent note to be too long
  if (subNoteLines.join("\n").length > 100000) {
    Zotero.Knowledge4Zotero.ZoteroViews.showProgressWindow(
      "Better Notes",
      "The linked note is too long. Import ignored."
    );
    return;
  }
  const templateText =
    await Zotero.Knowledge4Zotero.TemplateController.renderTemplateAsync(
      "[QuickImport]",
      "subNoteLines, subNoteItem, noteItem",
      [subNoteLines, subNoteItem, noteItem]
    );
  newLines.push(templateText);
  const newLineString = newLines.join("\n");
  const notifyFlag = Zotero.Promise.defer();
  const notifierName = "insertLinkWait";
  Zotero.Knowledge4Zotero.ZoteroNotifies.registerNotifyListener(
    notifierName,
    (event, type, ids, extraData) => {
      if (
        event === "modify" &&
        type === "item" &&
        ids.includes(noteItem.id)
      ) {
        notifyFlag.resolve();
        Zotero.Knowledge4Zotero.ZoteroNotifies.unregisterNotifyListener(notifierName);
      }
    }
  );
  await Zotero.Knowledge4Zotero.NoteUtils.modifyLineInNote(
    noteItem,
    (oldLine) => {
      Zotero.debug(oldLine);
      const params = Zotero.Knowledge4Zotero.NoteParse.parseParamsFromLink(link);
      const newLink = !params.ignore
        ? link + (link.includes("?") ? "&ignore=1" : "?ignore=1")
        : link;
      const linkIndex = Zotero.Knowledge4Zotero.NoteParse.parseLinkIndexInText(oldLine);
      Zotero.debug(linkIndex);
      return `${oldLine.slice(0, linkIndex[0])}${newLink}${oldLine.slice(
        linkIndex[1]
      )}\n${newLineString}`;
    },
    lineIndex,
    true
  );
  // wait the first modify finish
  await notifyFlag.promise;
  let hasAttachemnts = false;
  for (const _n of [subNoteItem, ...convertResult.subNotes]) {
    if (Zotero.Items.get(_n.getAttachments()).length) {
      hasAttachemnts = true;
      break;
    }
  }
  if (hasAttachemnts) {
    await Zotero.DB.executeTransaction(async () => {
      await Zotero.Notes.copyEmbeddedImages(subNoteItem, noteItem);
      for (const subNote of convertResult.subNotes) {
        await Zotero.Notes.copyEmbeddedImages(subNote, noteItem);
      }
    });
    await Zotero.Knowledge4Zotero.NoteUtils.scrollWithRefresh(
      Zotero.Knowledge4Zotero.NoteUtils.currentLine[noteItem.id]
    );
  }
})}

@GouPengyun
Copy link

现在这个方法插入几次笔记之后主笔记的文档层次就会被搞乱。原本设置的一级标题会莫名其妙的被变成引用的内容,如图。1677398112230

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants