From 0eee08855cfe82cca78d441659b84bdf4c8de96f Mon Sep 17 00:00:00 2001 From: lxsang Date: Thu, 13 Sep 2018 22:32:56 +0200 Subject: [PATCH] image insert --- OpenPage/build/debug/main.css | 2 +- OpenPage/build/debug/main.js | 70 +++++++++++++++++++++++++++++++---- OpenPage/coffees/main.coffee | 49 ++++++++++++++++++++++-- OpenPage/css/main.css | 2 +- 4 files changed, 109 insertions(+), 14 deletions(-) diff --git a/OpenPage/build/debug/main.css b/OpenPage/build/debug/main.css index 2873845..77073ce 100644 --- a/OpenPage/build/debug/main.css +++ b/OpenPage/build/debug/main.css @@ -34,7 +34,7 @@ afx-app-window[data-id="OpenPage"] afx-hbox[data-id="toolbox"] afx-button button height: 100%; } -afx-app-window[data-id="OpenPage"] afx-hbox[data-id="toolbox"] afx-button button:hover, afx-app-window[data-id="OpenPage"] afx-hbox[data-id="toolbox"] afx-button.btactive button +afx-app-window[data-id="OpenPage"] afx-hbox[data-id="toolbox"] afx-button button:hover, afx-app-window[data-id="OpenPage"] afx-hbox[data-id="toolbox"] afx-button button.btactive { border: 1px solid #759DC0; background-color: transparent; diff --git a/OpenPage/build/debug/main.js b/OpenPage/build/debug/main.js index c21f7bc..cc87a8b 100644 --- a/OpenPage/build/debug/main.js +++ b/OpenPage/build/debug/main.js @@ -8,7 +8,7 @@ main() { // load session class - if (!OpenPage.editorSession) { + if (!OpenPage.EditorSession) { require(["webodf/editor/EditorSession"], function(ES) { return OpenPage.EditorSession = ES; }); @@ -79,12 +79,10 @@ el.classList.add("notranslate"); this.userid = "localuser"; this.canvas = new odf.OdfCanvas(el); - this.documentChanged = function(e) { - return console.log(e); - }; - this.metaChanged = function(e) { - return console.log(e); - }; + this.documentChanged = function(e) {}; + //console.log e + this.metaChanged = function(e) {}; + //console.log e this.textStylingChanged = function(e) { return me.updateToolbar(e); }; @@ -124,6 +122,13 @@ me.eventSubscriptions.addFrameSubscription(me.editorSession, OpenPage.EditorSession.signalParagraphStyleModified, function() { return me.updateHyperlinkButtons(); }); + + //image controller + me.imageController = me.editorSession.sessionController.getImageController(); + //imageController.subscribe(gui.ImageController.enabledChanged, enableButtons) + + //text controller + me.textController = me.editorSession.sessionController.getTextController(); me.editorSession.sessionController.setUndoManager(new gui.TrivialUndoManager()); me.editorSession.sessionController.getUndoManager().subscribe(gui.UndoManager.signalDocumentModifiedChanged, me.documentChanged); me.editorSession.sessionController.getMetadataController().subscribe(gui.MetadataController.signalMetadataChanged, me.metaChanged); @@ -311,6 +316,53 @@ return this.hyperlinkController.removeHyperlinks(); } + undo(e) { + return this.editorSession.undo(); + } + + redo(e) { + return this.editorSession.redo(); + } + + image(e) { + var me; + me = this; + return this.openDialog("FileDiaLog", function(d, n, p) { + var fp; + fp = p.asFileHandler(); + return fp.asFileHandler().read(function(data) { + var blob, reader; + blob = new Blob([data], { + type: fp.info.mime + }); + reader = new FileReader(); + reader.onloadend = function() { + var hiddenImage; + if (reader.readyState !== 2) { + return me.error(__("Couldnt load image {0}", p)); + } + // insert the image to document + hiddenImage = new Image(); + hiddenImage.style.position = "absolute"; + hiddenImage.style.left = "-99999px"; + document.body.appendChild(hiddenImage); + hiddenImage.onload = function() { + var content; + content = reader.result.substring(reader.result.indexOf(",") + 1); + //insert image + me.textController.removeCurrentSelection(); + me.imageController.insertImage(fp.info.mime, content, hiddenImage.width, hiddenImage.height); + return document.body.removeChild(hiddenImage); + }; + return hiddenImage.src = reader.result; + }; + return reader.readAsDataURL(blob); + }, "binary"); + }, __("Select image file"), { + mimes: ["image/.*"] + }); + } + closeDocument() { var me, op; // finish editing @@ -354,7 +406,9 @@ return me.notify("Document closed"); }); me.session = void 0; - return me.directFormattingCtl = void 0; + me.directFormattingCtl = void 0; + me.textController = void 0; + return me.imageController = void 0; }); }); }); diff --git a/OpenPage/coffees/main.coffee b/OpenPage/coffees/main.coffee index 6d0887d..010fcd0 100644 --- a/OpenPage/coffees/main.coffee +++ b/OpenPage/coffees/main.coffee @@ -4,7 +4,7 @@ class OpenPage extends this.OS.GUI.BaseApplication main: () -> # load session class - if not OpenPage.editorSession + if not OpenPage.EditorSession require ["webodf/editor/EditorSession"], (ES) -> OpenPage.EditorSession = ES @eventSubscriptions = new core.EventSubscriptions() @@ -56,9 +56,9 @@ class OpenPage extends this.OS.GUI.BaseApplication @userid = "localuser" @canvas = new odf.OdfCanvas(el) @documentChanged = (e) -> - console.log e + #console.log e @metaChanged = (e) -> - console.log e + #console.log e @textStylingChanged = (e) -> me.updateToolbar e #@canvas.enableAnnotations(true, true) @@ -90,7 +90,14 @@ class OpenPage extends this.OS.GUI.BaseApplication me.eventSubscriptions.addFrameSubscription me.editorSession, OpenPage.EditorSession.signalCursorMoved, ()-> me.updateHyperlinkButtons() me.eventSubscriptions.addFrameSubscription me.editorSession, OpenPage.EditorSession.signalParagraphChanged, ()-> me.updateHyperlinkButtons() me.eventSubscriptions.addFrameSubscription me.editorSession, OpenPage.EditorSession.signalParagraphStyleModified, ()-> me.updateHyperlinkButtons() - + + #image controller + me.imageController = me.editorSession.sessionController.getImageController() + #imageController.subscribe(gui.ImageController.enabledChanged, enableButtons) + + #text controller + me.textController = me.editorSession.sessionController.getTextController() + me.editorSession.sessionController.setUndoManager new gui.TrivialUndoManager() me.editorSession.sessionController.getUndoManager().subscribe gui.UndoManager.signalDocumentModifiedChanged, me.documentChanged me.editorSession.sessionController.getMetadataController().subscribe gui.MetadataController.signalMetadataChanged, me.metaChanged @@ -220,6 +227,38 @@ class OpenPage extends this.OS.GUI.BaseApplication unlink: (e) -> @hyperlinkController.removeHyperlinks() + undo: (e) -> + @editorSession.undo() + + redo: (e) -> + @editorSession.redo() + + image: (e) -> + me = @ + @openDialog "FileDiaLog", (d, n, p) -> + fp = p.asFileHandler() + fp.asFileHandler().read (data) -> + blob = new Blob [data], { type: fp.info.mime } + reader = new FileReader() + reader.onloadend = () -> + return me.error __("Couldnt load image {0}", p) if reader.readyState isnt 2 + # insert the image to document + hiddenImage = new Image() + hiddenImage.style.position = "absolute" + hiddenImage.style.left = "-99999px" + document.body.appendChild hiddenImage + hiddenImage.onload = () -> + content = reader.result.substring(reader.result.indexOf(",") + 1) + #insert image + me.textController.removeCurrentSelection() + me.imageController.insertImage fp.info.mime, content, hiddenImage.width, hiddenImage.height + document.body.removeChild hiddenImage + hiddenImage.src = reader.result + + reader.readAsDataURL blob + , "binary" + , __("Select image file"), { mimes: ["image/.*"] } + closeDocument: () -> # finish editing return unless @editorSession and @session @@ -252,6 +291,8 @@ class OpenPage extends this.OS.GUI.BaseApplication me.notify "Document closed" me.session = undefined me.directFormattingCtl = undefined + me.textController = undefined + me.imageController = undefined # diff --git a/OpenPage/css/main.css b/OpenPage/css/main.css index 5affa7c..bbb69d4 100644 --- a/OpenPage/css/main.css +++ b/OpenPage/css/main.css @@ -33,7 +33,7 @@ afx-app-window[data-id="OpenPage"] afx-hbox[data-id="toolbox"] afx-button button height: 100%; } -afx-app-window[data-id="OpenPage"] afx-hbox[data-id="toolbox"] afx-button button:hover, afx-app-window[data-id="OpenPage"] afx-hbox[data-id="toolbox"] afx-button.btactive button +afx-app-window[data-id="OpenPage"] afx-hbox[data-id="toolbox"] afx-button button:hover, afx-app-window[data-id="OpenPage"] afx-hbox[data-id="toolbox"] afx-button button.btactive { border: 1px solid #759DC0; background-color: transparent;