Skip to content

Commit

Permalink
update: insert/import template
Browse files Browse the repository at this point in the history
resolve: #26
  • Loading branch information
xiangyu committed May 29, 2022
1 parent e9442aa commit 183fae3
Show file tree
Hide file tree
Showing 11 changed files with 447 additions and 92 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ https://user-images.githubusercontent.com/33902321/167992626-34adfd97-c2df-48b0-
- Foward-link to sub-notes and back-link to PDF attachments;
- Export archived notes in MarkDown/rich-text format.
- Export markdown with images is supported in v0.1.1
- Use templates to customize your note! [Document](./Template.md)

## Test Features

Expand Down
280 changes: 280 additions & 0 deletions Template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
# Template

v0.3.0 supports a new feature called _Template_

You can find it under the Workspace Window->Edit:
![image](https://user-images.githubusercontent.com/33902321/169189492-ab27b5ef-d6b2-4e4b-9035-2c11a91d53a1.png)

## Add a template

Click the _Edit Templates_ to open the editor.
![image](https://user-images.githubusercontent.com/33902321/169189605-e450702c-2336-463f-b157-600a198d987c.png)

Create a blank template, or from a note. Here are example templates for the test:

Copy The Name & template text to a new template and save it!

**Template Name must include one of these keywords:**

- Text: indicate it's a normal template
- Note: indicate it's a note template. Must select notes before inserting
- Item: indicate it's an item template. Must select items before inserting
- QuickInsert: The template used when insert a note link to main note. Only the template with name `[QuickInsert]` is used.
- QuickImport: The template used when import a sub-note to main note. Only the template with name `[QuickImport]` is used.

## Templates

Welcome to share & contribute your template!

### [Note] with meta-data

```js
<p><span style="background-color: #ffd40080">Note: ${link}</span></p>
${topItem?`<p>Title: ${topItem.getField('title')}</p>
<p>Author: ${topItem.getField('firstCreator')}</p>
<p>Date: ${topItem.getField('date')}</p>`:''}
```

### [Item] meta-data

```js
<h2>Item Meta Data:</h2>
<p>Title: ${topItem.getField('title')}</p>
<p>Author: ${topItem.getField('firstCreator')}</p>
<p>Date: ${topItem.getField('date')}</p>
```

### [Item] item-notes with metadata:

shared by @zyx335588
![image](https://user-images.githubusercontent.com/33902321/169704517-14faa474-0273-4357-99af-982f48576533.png)

```js
<h1>${topItem.getField('title')}</h1>
<h2 style="color:red; background-color: #efe3da;">💡 Meta Data</h2>
<table>
<tr>
<th style="background-color:#dbeedd;">
<p style="text-align: right">Title </p>
</th>
<td style="background-color:#dbeedd;">
${topItem.getField('title')}
</td>
</tr>
<tr>
<th style="background-color:#f3faf4;">
<p style="text-align: right">Journal </p>
</th>
<td style="background-color:#f3faf4;">
${topItem.getField('publicationTitle')}
</td>
</tr>
<tr>
<th style="background-color:#dbeedd;">
<p style="text-align: right">1<sup>st</sup> Author </p>
</th>
<td style="background-color:#dbeedd;">
${topItem.getField('firstCreator')}
</td>
</tr>
<tr>
<th style="background-color:#f3faf4;">
<p style="text-align: right">Authors </p>
</th>
<td style="background-color:#f3faf4;">
${topItem.getCreators().map((v)=>v.firstName+" "+v.lastName).join("; ")}
</td>
</tr>
<tr>
<th style="background-color:#dbeedd;">
<p style="text-align: right">Pub. date </p>
</th>
<td style="background-color:#dbeedd;">
${topItem.getField('date')}
</td>
</tr>
<tr>
<th style="background-color:#f3faf4;">
<p style="text-align: right">DOI </p>
</th>
<td style="background-color:#f3faf4;">
<a href="https://doi.org/${topItem.getField('DOI')}">${topItem.getField('DOI')}</a>
</td>
</tr>
<tr>
<th style="background-color:#dbeedd;">
<p style="text-align: right">Archive </p>
</th>
<td style="background-color:#dbeedd;">
${topItem.getField('archive')}
</td>
</tr>
<tr>
<th style="background-color:#f3faf4;">
<p style="text-align: right">Archive Location </p>
</th>
<td style="background-color:#f3faf4;">
${topItem.getField('archiveLocation')}
</td>
</tr>
<tr>
<th style="background-color:#dbeedd;">
<p style="text-align: right">Call No. </p>
</th>
<td style="background-color:#dbeedd;">
${topItem.getField('callNumber')}
</td>
</tr>
</table>
${itemNotes.map((noteItem)=>{
const noteLine = `<h2 style="color:red; background-color: #efe3da;">📜 Note: <a href="${Zotero.Knowledge4Zotero.knowledge.getNoteLink(noteItem)
}" rel="noopener noreferrer nofollow">${noteItem.key}</a></h2>
<blockquote>
${noteItem.getNote()}
<p style="background-color: pink;"><strong>Merge Date: </strong> ${new Date().toISOString().substr(0,10)+" "+ new Date().toTimeString()}</p>
</blockquote>
<p style="color:red; background-color: #efe3da;"><strong>📝 Comments</strong></p>
<blockquote>
<p>Make your comments</p>
<p></p>
</blockquote>`;
copyNoteImage(noteItem);
return noteLine;
}).join("\n")}
```

### [Text] today

```js
<h1>TODO: ${new Date().toLocaleDateString()}</h1>
<h2>Tasks</h2>
<ul>
<li>
Read Paper 1
</li>
<li>
Do some experiments
</li>
</ul>
<blockquote>
<p>Insert more items with meta-data in workspace window-&gt;Edit</p>
</blockquote>
<p></p>
<h2>Done Tasks</h2>
<p></p>
<h2>Todo Tomorrow</h2>
<p></p>
</div>
```

## Use a template

You can insert templates in 3 ways: `Text`, `Note`, and `Item`.

Templates with these keywords will show in the corresponding menu popup.

## Create a template

### Text

> variables: no special variables
> A pure text template.
### Item

> variables: topItem
> Select items and use `topItem`(a Zotero Item Object) to access its metadata. For example:
```js
<p>${topItem.getField("title")}</p>
```

Will be compiled to:

> YOLO-LITE: A Real-Time Object Detection Algorithm Optimized for Non-GPU Computers
### Note

> variables: topItem, noteItem, link
> Select notes and use `topItem`(a Zotero Item Object) to access its metadata, `noteItem` to access the note. For example:
```js
<h1>Note: ${noteItem.getNoteTitle()}</h1>
${link}
<p>${topItem.getField('title')}</p>
```

Will be compiled to:

> # Note: Sensors in Papers
>
> [Sensors in Papers](http://:zotero://note/u/GCY9G2PZ/)
> YOLO-LITE: A Real-Time Object Detection Algorithm Optimized for Non-GPU Computers
### QuickInsert

> variables: link: string, subNoteItem, noteItem
### QuickImport

> variables: subNoteLines: string[], subNoteItem, noteItem
## API and Variables

This section is for developers who want to create a new template. Basic coding knowledge is required.
To test & run scripts quickly, open Tools->Developer->Run Javascript.

### Item Fields

The `noteItem` and `topItem` is a Zotero Item object. The general data can be accessed using the `getField()` method.

For example: `topItem.getField('title')` will return the title of the `topItem`.

```ts
// Get Item Fields
getField: (void)=>string;

// Get Authors
getCreators: (void)=>{
fieldMode: number,
firstName: string, // may be empty
lastName: string,
creatorTypeID: number,
}[];
```

Find available fields of the selected item with the code below:

```js
const item = ZoteroPane.getSelectedItems().shift();
const usedFields = item.getUsedFields();
Zotero.ItemFields.getAll()
.filter((e) => usedFields.indexOf(e.id) >= 0)
.map((e) => e.name);
```

The result is like this (depending on the item you select):

```JSON
[
"0": "title"
"1": "date"
"2": "language"
"3": "shortTitle"
"4": "libraryCatalog"
"5": "url"
"6": "accessDate"
"7": "pages"
"8": "conferenceName"
]
```

### Note Fields

```ts
// Get Note Text in HTML
getNote: (void)=>string;

// Get Note Title
getNoteTitle: (void)=>string;
```
65 changes: 37 additions & 28 deletions addon/chrome/content/export.xul
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,41 @@

<dialog id="betternotes-export-dialog" windowtype="betternotes-export" title="&zotero.__addonRef__.export.title;" orient="vertical" width="300" height="300" buttons="cancel,accept" ondialogaccept="Zotero.Knowledge4Zotero.export.doAccept();" onload="Zotero.Knowledge4Zotero.export.doLoad(window);" onunload="Zotero.Knowledge4Zotero.export.doUnload();" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" xmlns:html="http://www.w3.org/1999/xhtml" style="padding:2em" persist="screenX screenY width height">
<script src="chrome://zotero/content/include.js" />
<box flex="1">
<rows flex="1">
<row>
<checkbox id="__addonRef__-export-enablefile" checked="true" />
<label value="&zotero.__addonRef__.export.file.enable.label;" />
</row>
<row>
<checkbox id="__addonRef__-export-embedLink" checked="true" />
<label value="&zotero.__addonRef__.export.link.enable.label;" />
</row>
<row>
<checkbox id="__addonRef__-export-embedImage" checked="true" />
<label value="&zotero.__addonRef__.export.image.enable.label;" />
</row>
<separator></separator>
<row>
<checkbox id="__addonRef__-export-enablenote" checked="false" />
<label value="&zotero.__addonRef__.export.note.enable.label;" />
</row>
<row>
<checkbox id="__addonRef__-export-enablecopy" checked="false" />
<label value="&zotero.__addonRef__.export.copy.enable.label;" />
</row>
<row>
<html:div style="position: fixed">&zotero.__addonRef__.export.help.label;</html:div>
</row>
</rows>
</box>
<vbox flex="1">
<groupbox flex="1">
<caption label="&zotero.__addonRef__.export.option.label;"></caption>
<rows flex="1">
<row>
<checkbox id="__addonRef__-export-embedLink" checked="true" />
<label value="&zotero.__addonRef__.export.link.enable.label;" />
</row>
<row>
<checkbox id="__addonRef__-export-enablenote" checked="false" />
<label value="&zotero.__addonRef__.export.note.enable.label;" />
</row>
</rows>
</groupbox>
<groupbox flex="1">
<caption label="&zotero.__addonRef__.export.markdown.label;"></caption>
<rows flex="1">
<row>
<checkbox id="__addonRef__-export-enablefile" checked="true" />
<label value="&zotero.__addonRef__.export.file.enable.label;" />
</row>
<row>
<checkbox id="__addonRef__-export-embedImage" checked="true" />
<label value="&zotero.__addonRef__.export.image.enable.label;" />
</row>
</rows>
</groupbox>
<groupbox flex="1">
<caption label="&zotero.__addonRef__.export.richtext.label;"></caption>
<rows flex="1">
<row>
<checkbox id="__addonRef__-export-enablecopy" checked="false" />
<label value="&zotero.__addonRef__.export.copy.enable.label;" />
</row>
</rows>
</groupbox>
</vbox>
</dialog>
6 changes: 4 additions & 2 deletions addon/chrome/content/template.xul
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@

<keyset>
<key id="key_save" key="S" modifiers="accel" command="cmd_save" />
<key id="key_help" key="H" modifiers="accel" command="cmd_help" />
<key id="key_close" key="W" modifiers="accel" command="cmd_close" />
</keyset>
<command id="cmd_save" oncommand="Zotero.Knowledge4Zotero.template.saveSelectedTemplate();" />
<command id="cmd_help" oncommand="Zotero.launchURL('https://github.com/windingwind/zotero-better-notes/Templates.md');" />
<command id="cmd_close" oncommand="window.close();" />

<hbox flex="1">
Expand All @@ -49,9 +51,9 @@
<row style="margin: 10px 0 10px 0;">
<button id="create-template" label="&zotero.__addonRef__.template.create.label;" oncommand="Zotero.Knowledge4Zotero.template.createTemplate();"></button>
<button id="import-template" label="&zotero.__addonRef__.template.import.label;" oncommand="Zotero.Knowledge4Zotero.template.importNoteTemplate();"></button>
<button id="save-template" label="&zotero.__addonRef__.template.save.label;" oncommand="Zotero.Knowledge4Zotero.template.saveSelectedTemplate();"></button>
<button id="save-template" label="&zotero.__addonRef__.template.save.label;" command="command_save" key="key_save" accesskey="S"></button>
<button id="delete-template" label="&zotero.__addonRef__.template.delete.label;" oncommand="Zotero.Knowledge4Zotero.template.deleteSelectedTemplate();"></button>
<!-- <button id="help" label="&helpMenu.label;"></button> -->
<button id="help" label="&helpMenu.label;" command="cmd_help" key="key_help" accesskey="H"></button>
</row>
</rows>
</hbox>
Expand Down
Loading

0 comments on commit 183fae3

Please sign in to comment.