diff --git a/mix/AppContext.cpp b/mix/AppContext.cpp index 4f7c494e8..1aa43a119 100644 --- a/mix/AppContext.cpp +++ b/mix/AppContext.cpp @@ -50,6 +50,7 @@ AppContext::AppContext(QQmlApplicationEngine* _engine) m_codeModel.reset(new CodeModel(this)); m_clientModel.reset(new ClientModel(this)); m_fileIo.reset(new FileIo()); + connect(QApplication::clipboard(), &QClipboard::dataChanged, [this] { emit clipboardChanged();}); } AppContext::~AppContext() @@ -106,6 +107,12 @@ void AppContext::displayMessageDialog(QString _title, QString _message) QMetaObject::invokeMethod(dialogWin, "open"); } +QString AppContext::clipboard() const +{ + QClipboard *clipboard = QApplication::clipboard(); + return clipboard->text(); +} + void AppContext::toClipboard(QString _text) { QClipboard *clipboard = QApplication::clipboard(); diff --git a/mix/AppContext.h b/mix/AppContext.h index 31ecd3198..268771207 100644 --- a/mix/AppContext.h +++ b/mix/AppContext.h @@ -48,6 +48,7 @@ class FileIo; class AppContext: public QObject { Q_OBJECT + Q_PROPERTY(QString clipboard READ clipboard WRITE toClipboard NOTIFY clipboardChanged) public: AppContext(QQmlApplicationEngine* _engine); @@ -64,10 +65,13 @@ public: void displayMessageDialog(QString _title, QString _message); /// Copy text to clipboard Q_INVOKABLE void toClipboard(QString _text); + /// Get text from clipboard + QString clipboard() const; signals: /// Triggered once components have been loaded void appLoaded(); + void clipboardChanged(); private: QQmlApplicationEngine* m_applicationEngine; //owned by app diff --git a/mix/qml/WebCodeEditor.qml b/mix/qml/WebCodeEditor.qml index b5066b1bf..a1c273698 100644 --- a/mix/qml/WebCodeEditor.qml +++ b/mix/qml/WebCodeEditor.qml @@ -4,6 +4,7 @@ import QtQuick.Layouts 1.0 import QtQuick.Controls.Styles 1.1 import CodeEditorExtensionManager 1.0 import QtWebEngine 1.0 +import QtWebEngine.experimental 1.0 Item { signal editorTextChanged @@ -29,6 +30,18 @@ Item { return currentText; } + function syncClipboard() { + if (Qt.platform.os == "osx") { + var text = appContext.clipboard; + editorBrowser.runJavaScript("setClipboardBase64(\"" + Qt.btoa(text) + "\")"); + } + } + + Connections { + target: appContext + onClipboardChanged: syncClipboard() + } + anchors.top: parent.top id: codeEditorView anchors.fill: parent @@ -36,6 +49,7 @@ Item { id: editorBrowser url: "qrc:///qml/html/codeeditor.html" anchors.fill: parent + experimental.settings.javascriptCanAccessClipboard: true onJavaScriptConsoleMessage: { console.log("editor: " + sourceID + ":" + lineNumber + ":" + message); } @@ -47,6 +61,7 @@ Item { setText(currentText, currentMode); runJavaScript("getTextChanged()", function(result) { }); pollTimer.running = true; + syncClipboard(); } } diff --git a/mix/qml/html/cm/codemirror.js b/mix/qml/html/cm/codemirror.js index 9c56d7325..38fe63dd4 100644 --- a/mix/qml/html/cm/codemirror.js +++ b/mix/qml/html/cm/codemirror.js @@ -29,7 +29,7 @@ var ie = ie_upto10 || ie_11up; var ie_version = ie && (ie_upto10 ? document.documentMode || 6 : ie_11up[1]); var webkit = /WebKit\//.test(navigator.userAgent); - var qtwebkit = webkit && /Qt\/\d+\.\d+/.test(navigator.userAgent); + var qtwebkit = true; var chrome = /Chrome\//.test(navigator.userAgent); var presto = /Opera\//.test(navigator.userAgent); var safari = /Apple Computer/.test(navigator.vendor); diff --git a/mix/qml/html/codeeditor.js b/mix/qml/html/codeeditor.js index 5b3e5450f..ee93d5bf6 100644 --- a/mix/qml/html/codeeditor.js +++ b/mix/qml/html/codeeditor.js @@ -6,6 +6,7 @@ var editor = CodeMirror(document.body, { autofocus: true, }); + editor.setOption("theme", "solarized dark"); editor.setOption("indentUnit", 4); editor.setOption("indentWithTabs", true); @@ -18,11 +19,24 @@ editor.on("change", function(eMirror, object) { }); +var mac = /Mac/.test(navigator.platform); +if (mac === true) { +editor.setOption("extraKeys", { + "Cmd-V": function(cm) { + cm.replaceSelection(clipboard); + }, + "Cmd-X": function(cm) { + window.document.execCommand("cut"); + }, + "Cmd-C": function(cm) { + window.document.execCommand("copy"); + }}); +} + getTextChanged = function() { return editor.changeRegistered; }; - getText = function() { editor.changeRegistered = false; return editor.getValue(); @@ -42,3 +56,7 @@ setText = function(text) { setMode = function(mode) { this.editor.setOption("mode", mode); }; + +setClipboardBase64 = function(text) { + clipboard = window.atob(text); +};