Skip to content

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

woofmark editor main frame implementation

Borislav Sapundzhievf40e5f6c50d4
diff --git a/data/ext/editorNext/editor.js b/data/ext/editorNext/editor.js
index a326f6438..8fdf5b751 100644
--- a/data/ext/editorNext/editor.js
+++ b/data/ext/editorNext/editor.js
@@ -1,7 +1,6 @@
"use strict";
var isCordova = parent.isCordova;
-var $htmlEditor;
var editor = null;
function initEditor() {
@@ -24,10 +23,10 @@ function getContentVersion() {
}
function setContent(content) {
+
resetContentVersion();
- $htmlEditor = $('#htmlEditor');
- $htmlEditor.find('textarea').val(content);
-
+ $('#htmlEditor').find('textarea').val(content);
+
initEditor();
}
\ No newline at end of file
diff --git a/data/ext/editorNext/extension.js b/data/ext/editorNext/extension.js
index 51bc7e8cb..ec63a5448 100644
--- a/data/ext/editorNext/extension.js
+++ b/data/ext/editorNext/extension.js
@@ -9,46 +9,100 @@ define(function(require, exports, module) {
console.log("Loading editorNext");
- console.log("Loading editorHTML");
-
var extensionTitle = "HTML Editor";
- var extensionID = "editorHTML"; // ID should be equal to the directory name where the ext. is located
+ var extensionID = "editorNext"; // ID should be equal to the directory name where the ext. is located
var extensionType = "editor";
var extensionSupportedFileTypes = ["html", "htm"];
var TSCORE = require("tscore");
- var extensionDirectory = TSCORE.Config.getExtensionPath() + "/" + exports.id;
+ var extensionDirectory = TSCORE.Config.getExtensionPath() + "/" + extensionID//exports.id;
+ var currentContent;
exports.init = function(filePath, elementID) {
console.log("Initalization editorNext");
- var $parent = $('#' + elementID);
-
- alert(filePath);
- var newDiv = $("<div />");
- var textArea = $('<textarea style="padding-left:100px" />');
- textArea.text("Hello world");
- newDiv.append(textArea);
- $parent.append(newDiv);
-
- require(["ext/editorNext/woofmark/woofmark.min"], function() {
- alert(woofmark);
- woofmark(textArea, {
- //parseMarkdown: megamark,
- //parseHTML: domador
- });
- };
+ var $containerElement = $('#' + elementID);
+ $containerElement.empty();
+ $containerElement.css("background-color", "white");
+
+ var extPath = extensionDirectory + "/index.html";
+ $containerElement.append($('<iframe>', {
+ id: "iframeViewer",
+ sandbox: "allow-same-origin allow-scripts",
+ scrolling: "no",
+ style: "background-color: white; overflow: hidden;",
+ src: extPath,
+ "nwdisable": "",
+ "nwfaketop": ""
+ }).load(function() {
+ TSCORE.IO.loadTextFile(filePath);
+ }));
+
+ };
exports.viewerMode = function() {
console.log("viewerMode not supported on this extension");
};
exports.setContent = function(content) {
- console.log("setContent not supported on this extension");
+ currentContent = content;
+
+ var bodyRegex = /\<body[^>]*\>([^]*)\<\/body/m; // jshint ignore:line
+ var bodyContent;
+
+ try {
+ bodyContent = content.match(bodyRegex)[1];
+ } catch (e) {
+ console.log("Error parsing HTML document. " + e);
+ TSCORE.FileOpener.closeFile(true);
+ TSCORE.showAlertDialog("Probably a body tag was not found in the document. Document will be closed.", "Error parsing HTML document");
+ }
+
+ // removing all scripts from the document
+ var cleanedBodyContent = bodyContent.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, "");
+
+ var contentWindow = document.getElementById("iframeViewer").contentWindow;
+ if (typeof contentWindow.setContent === "function") {
+ contentWindow.setContent(cleanedBodyContent);
+ } else {
+ alert("TODO optimize setTimeout");
+ }
+
};
exports.getContent = function() {
- console.log("getContent not supported on this extension");
+
+ var content = $("#iframeViewer").contents().find("textarea").val();
+
+ // removing all scripts from the document
+ var cleanedContent = content.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, "");
+
+ // saving all images as png in base64 format
+ var match,
+ urls = [],
+ imgUrl = "",
+ rex = /<img.*?src="([^">]*\/([^">]*?))".*?>/g;
+
+ while (match = rex.exec(cleanedContent)) {
+ imgUrl = match[1];
+ console.log("URLs: " + imgUrl);
+ urls.push([imgUrl, TSCORE.Utils.getBase64Image(imgUrl)]);
+ }
+
+ urls.forEach(function(dataURLObject) {
+ if (dataURLObject[1].length > 7) {
+ cleanedContent = cleanedContent.split(dataURLObject[0]).join(dataURLObject[1]);
+ }
+ //console.log(dataURLObject[0]+" - "+dataURLObject[1]);
+ });
+ // end saving all images
+
+ cleanedContent = "<body>" + cleanedContent + "</body>";
+
+ var htmlContent = currentContent.replace(/\<body[^>]*\>([^]*)\<\/body>/m, cleanedContent); // jshint ignore:line
+ //console.log("Final html "+htmlContent);
+ //resetContentVersion();
+ return htmlContent;
};
});
diff --git a/data/ext/editorNext/index.html b/data/ext/editorNext/index.html
index f57884c20..ea41490aa 100644
--- a/data/ext/editorNext/index.html
+++ b/data/ext/editorNext/index.html
@@ -8,18 +8,16 @@
<script type="text/javascript" src="../../libs/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="woofmark/megamark.js"></script>
<script type="text/javascript" src="woofmark/domador.js"></script>
- <script type="text/javascript" src="woofmark/woofmark.js"></script>
+ <script type="text/javascript" src="woofmark/woofmark.min.js"></script>
- <!--
- <link rel="stylesheet" href="extension.css" />
+ <link rel="stylesheet" href="editor.css" />
<link rel="stylesheet" href="../../libs/bootstrap/css/bootstrap.css" />
<link rel="stylesheet" href="../../libs/font-awesome/css/font-awesome.css" />
- -->
<link rel="stylesheet" href="woofmark/woofmark.min.css" />
</head>
<body>
- <div id="htmlEditor">
+ <div id="htmlEditor" >
<textarea></textarea>
</div>
</body>