Skip to content

TagSpaces is an offline, open source, document manager with tagging support

CRUD meta tags

Borislav Sapundzhiev257452f8526e
diff --git a/data/js/core.api.js b/data/js/core.api.js
index 71a856df3..2c1342703 100644
--- a/data/js/core.api.js
+++ b/data/js/core.api.js
@@ -571,6 +571,26 @@ define(function(require, exports, module) {
return metaObj;
}
+ function saveMetaData(filePath, metaData) {
+
+ var metaFilePath = findMetaFilebyPath(filePath, exports.metsFileExt);
+
+ if(!metaFilePath) {
+ var name = exports.Utils.baseName(filePath) + "." + exports.metsFileExt;
+ metaFilePath = exports.currentPath + exports.dirSeparator +
+ exports.metaFolder + exports.dirSeparator + name;
+
+ var entry = {
+ "name": name,
+ "isFile": true,
+ "path": metaFilePath,
+ };
+ exports.metaFileList.push(entry);
+ }
+ var content = JSON.stringify(metaData);
+ exports.IO.saveTextFile(metaFilePath, content, true, true);
+ }
+
// Proxying applications parts
exports.Config = tsSettings;
exports.IO = tsIOApi;
@@ -681,4 +701,5 @@ define(function(require, exports, module) {
exports.metaFileList = metaFileList;
exports.findMetaFilebyPath = findMetaFilebyPath;
exports.findMetaObjectFromFileList = findMetaObjectFromFileList;
+ exports.saveMetaData = saveMetaData;
});
diff --git a/data/js/settings.default.js b/data/js/settings.default.js
index bf7534ee8..2e56d7501 100644
--- a/data/js/settings.default.js
+++ b/data/js/settings.default.js
@@ -33,7 +33,8 @@ define(function (require, exports, module) {
"ootbViewers": [ "viewerBrowser", "viewerMD", "viewerImage", "viewerPDF", "editorText", "viewerText", "editorODF", "viewerURL", "viewerHTML", "viewerMHTML", "editorJSON", "viewerZIP", "viewerEPUB", "viewerAudioVideo" ],
"ootbEditors": [ "editorHTML", "editorText", "editorODF", "editorJSON", "editorNext" ],
"perspectives": [{ 'id': 'perspectiveList' }, { 'id': 'perspectiveGrid' }, { 'id': 'perspectiveGraph' }],
- "interfaceLanguage": "en_US",
+ "interfaceLanguage": "en_US",
+ "writeTagsToFile": false,
"supportedLanguages": [
{ "iso": "en_US", "title": "English" },
{ "iso": "de_DE", "title": "Deutsch (German)" },
diff --git a/data/js/tagutils.js b/data/js/tagutils.js
index 03acbb969..b9cec37e3 100644
--- a/data/js/tagutils.js
+++ b/data/js/tagutils.js
@@ -332,8 +332,44 @@ define(function(require, exports, module) {
return newFileName;
}
+ function addMetaTags(filePath, tags) {
+ var metaObj = TSCORE.findMetaObjectFromFileList(filePath);
+ if (!metaObj) {
+ metaObj = {
+ thumbnailPath: "",
+ metaData: null,
+ };
+ }
+
+ if (!metaObj.metaData) {
+ metaObj.metaData = {
+ tags: []
+ };
+ }
+
+ tags.forEach(function(element) {
+ var newTag = {
+ "title": element,
+ "type":"plain",
+ "titleUI": element,
+ "icon":"",
+ "style":""
+ };
+ metaObj.metaData.tags.push(newTag);
+ });
+
+ TSCORE.saveMetaData(filePath, metaObj.metaData);
+ TSCORE.PerspectiveManager.updateFileUI(filePath, filePath);
+ }
+
function writeTagsToFile(filePath, tags) {
console.log('Add the tags to: ' + filePath);
+
+ if (TSCORE.Config.DefaultSettings.writeTagsToFile !== true) {
+ addMetaTags(filePath, tags);
+ return;
+ }
+
var fileName = extractFileName(filePath);
var containingDirectoryPath = extractContainingDirectoryPath(filePath);
var extractedTags = extractTags(filePath);
@@ -430,9 +466,27 @@ define(function(require, exports, module) {
TSCORE.IO.renameFile(filePath, containingDirectoryPath + TSCORE.dirSeparator + newFileName);
}
+ function reanmeMetaTag(filePath, oldTag, newTag) {
+ var metaObj = TSCORE.findMetaObjectFromFileList(filePath);
+ if(metaObj) {
+ metaObj.metaData.tags.forEach(function(tag , index) {
+ if(tag.title === oldTag) {
+ tag.title = newTag;
+ }
+ });
+ TSCORE.saveMetaData(filePath, metaObj.metaData);
+ TSCORE.PerspectiveManager.updateFileUI(filePath, filePath);
+ }
+ }
// Replaces a tag with a new one
function renameTag(filePath, oldTag, newTag) {
console.log('Rename tag for file: ' + filePath);
+
+ if (TSCORE.Config.DefaultSettings.writeTagsToFile !== true) {
+ reanmeMetaTag(filePath, oldTag, newTag);
+ return;
+ }
+
var fileName = extractFileName(filePath);
var containingDirectoryPath = extractContainingDirectoryPath(filePath);
var extractedTags = extractTags(filePath);
@@ -460,10 +514,7 @@ define(function(require, exports, module) {
return true;
}
- // Removing a tag from a filename
- function removeTag(filePath, tagName) {
- console.log('Removing tag: ' + tagName + ' from ' + filePath);
-
+ function removeMetaTag(filePath, tagName) {
var metaObj = TSCORE.findMetaObjectFromFileList(filePath);
if(metaObj) {
metaObj.metaData.tags.forEach(function(tag , index) {
@@ -478,6 +529,15 @@ define(function(require, exports, module) {
}
TSCORE.PerspectiveManager.updateFileUI(filePath, filePath);
}
+ }
+
+ // Removing a tag from a filename
+ function removeTag(filePath, tagName) {
+ console.log('Removing tag: ' + tagName + ' from ' + filePath);
+
+ if (TSCORE.Config.DefaultSettings.writeTagsToFile !== true) {
+ removeMetaTag(filePath, tagName);
+ }
var fileName = extractFileName(filePath);
var containingDirectoryPath = extractContainingDirectoryPath(filePath);
diff --git a/data/node-webkit/node-webkit.api.js b/data/node-webkit/node-webkit.api.js
index 591b88980..7cf008a14 100644
--- a/data/node-webkit/node-webkit.api.js
+++ b/data/node-webkit/node-webkit.api.js
@@ -531,40 +531,6 @@ define(function(require, exports, module) {
TSCORE.metaFileList = anotatedDirList;
readyCallback(anotatedDirList);
});
-
- /*var anotatedDirList = [];
- console.log("getDirectoryMetaInformation directory: " + dirPath);
- try {
- fs.readdir(dirPath, function(error, dirList) {
- if (error) {
- console.log("Listing directory: " + dirPath + " failed " + error);
- readyCallback(anotatedDirList);
- return;
- }
- for (var i = 0; i < dirList.length; i++) {
- var path = dirPath + TSCORE.dirSeparator + dirList[i];
- var stats = fs.lstatSync(path);
- if (stats !== undefined) {
- //console.log('stats: ' + JSON.stringify(stats));
- anotatedDirList.push({
- "name": dirList[i],
- "isFile": stats.isFile(),
- "size": stats.size,
- "lmdt": stats.mtime,
- "path": path
- });
- }
- }
- //TSPOSTIO.listDirectory(anotatedDirList);
- TSCORE.metaFileList = anotatedDirList;
- if(readyCallback) {
- readyCallback(anotatedDirList);
- }
- });
- } catch (ex) {
- console.error("Listing directory " + dirPath + " failed " + ex);
- readyCallback(anotatedDirList);
- }*/
};
var deleteElement = function(path) {