danny0838 / firefox-scrapbook

ScrapBook X – a legacy Firefox add-on that captures web pages to local device for future retrieval, organization, annotation, and edit.
Mozilla Public License 2.0
324 stars 65 forks source link

Some additions to create notes pages #280

Open mseliger opened 5 years ago

mseliger commented 5 years ago

I've have looked at the source code of ScrapBook X and I've made some additions for better working with New Note Pages.

Source: scrapbook.js Function: createNoteX: function(aAsChild)

createNoteX: function(aAsChild) {
    sbSearchService.exit();
    // create item
    var newItem = sbCommonUtils.newItem(sbCommonUtils.getTimeStamp());
    const now = new Date();
    // ask for note page title, msel 20190318
    var notextitle = prompt("Title", "New Note Page");
    if (notextitle == null || notextitle == "") notextitle = "New Note Page";
    newItem.id = sbDataSource.identify(newItem.id);
    newItem.title = sbCommonUtils.lang("DEFAULT_NOTEX");
    newItem.title = notextitle;
    newItem.type = "notex";
    newItem.chars = "UTF-8";
    // check the template file, create one if not exist
    var template = sbCommonUtils.getScrapBookDir().clone();
    template.append("notex_template.html");
    if ( !template.exists() ) sbCommonUtils.saveTemplateFile("chrome://scrapbook/skin/notex_template.html", template);
    // create content
    var dir = sbCommonUtils.getContentDir(newItem.id);
    var html = dir.clone();
    html.append("index.html");
    //new variables NOTE_ID and NOTE_DATE, msel20190220
    var tpl = {
        NOTE_TITLE: newItem.title,
        SCRAPBOOK_DIR: "../..",
        DATA_DIR: ".",
        NOTE_ID: newItem.id,
        NOTE_DATE: now.toDateString(),
    };
    var content = sbCommonUtils.readFile(template, "UTF-8");
    content = content.replace(/<%([\w_]+)%>/g, function(){
        var label = arguments[1];
        if (tpl[label]) return tpl[label];
        return "";
    });
    sbCommonUtils.writeFile(html, content, newItem.chars);
    sbCommonUtils.writeIndexDat(newItem);
    // add resource
    var newRes = this.addNewResource(newItem, null, aAsChild);
    // open and edit the new notex
    sbController.open(newRes, false);
},

Now, if you create a new note page your asked about a name for the note page. And in addition you can use the new variables NOTE_ID and NOTE_DATE in the notex_template.html.

Here is an example:

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title data-sb-obj="title"><%NOTE_ID%>&nbsp;<%NOTE_TITLE%></title>
  <meta name="viewport" content="width=device-width">
</head>
<body>
<p><%NOTE_TITLE%></p>
<br/>
<p></p>
<hr/>
<b>Archive: </b>ScrapBook&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>ID: </b><%NOTE_ID%><br/>
<b>Date: </b><%NOTE_DATE%>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Changed: </b><%NOTE_DATE%><br/>
<p><b>Links: </b>&nbsp;</p>
<p><b>Attachments: </b>&nbsp;</p>
<p><b>Notes: </b>&nbsp;</p>
<hr/>
</body>
</html>

Perhaps you can add the code to scrapbook.js.