[evolution/wip/mcrha/webkit-jsc-api: 210/292] Miscellaneous basic changes around the webkit-editor extension



commit fa3c8631830bf40e55ff930d3062b9e4f4784535
Author: Milan Crha <mcrha redhat com>
Date:   Tue Oct 22 15:54:17 2019 +0200

    Miscellaneous basic changes around the webkit-editor extension

 data/webkit/CMakeLists.txt                         |  3 +
 data/webkit/e-convert.js                           | 17 +++++
 data/webkit/e-editor.js                            | 24 +++++++
 data/webkit/e-selection.js                         | 17 +++++
 data/webkit/e-undo-redo.js                         | 29 ++++++++-
 data/webkit/e-web-view.js                          | 17 +++++
 .../webkit-editor/web-extension/CMakeLists.txt     |  5 +-
 .../web-extension/e-editor-web-extension.c         | 74 ++++++++++++++++++++++
 8 files changed, 182 insertions(+), 4 deletions(-)
---
diff --git a/data/webkit/CMakeLists.txt b/data/webkit/CMakeLists.txt
index 37832ac9df..3397bd03c7 100644
--- a/data/webkit/CMakeLists.txt
+++ b/data/webkit/CMakeLists.txt
@@ -1,5 +1,8 @@
 set(DATA_FILES
        e-convert.js
+       e-editor.js
+       e-selection.js
+       e-undo-redo.js
        e-web-view.js
        webview.css
        webview-print.css
diff --git a/data/webkit/e-convert.js b/data/webkit/e-convert.js
index 4e67b92dc2..8fa7c0f37f 100644
--- a/data/webkit/e-convert.js
+++ b/data/webkit/e-convert.js
@@ -1,3 +1,20 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2019 Red Hat (www.redhat.com)
+ *
+ * This library is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
 'use strict';
 
 /* semi-convention: private functions start with lower-case letter,
diff --git a/data/webkit/e-editor.js b/data/webkit/e-editor.js
new file mode 100644
index 0000000000..dee4c83979
--- /dev/null
+++ b/data/webkit/e-editor.js
@@ -0,0 +1,24 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2019 Red Hat (www.redhat.com)
+ *
+ * This library is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+'use strict';
+
+/* semi-convention: private functions start with lower-case letter,
+   public functions start with upper-case letter. */
+
+var EvoEditor = {
+};
diff --git a/data/webkit/e-selection.js b/data/webkit/e-selection.js
index 80c7b5c5d6..0b91b33970 100644
--- a/data/webkit/e-selection.js
+++ b/data/webkit/e-selection.js
@@ -1,3 +1,20 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2019 Red Hat (www.redhat.com)
+ *
+ * This library is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
 'use strict';
 
 /* semi-convention: private functions start with lower-case letter,
diff --git a/data/webkit/e-undo-redo.js b/data/webkit/e-undo-redo.js
index 89c46c6326..9ecc3b9ad5 100644
--- a/data/webkit/e-undo-redo.js
+++ b/data/webkit/e-undo-redo.js
@@ -1,3 +1,20 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2019 Red Hat (www.redhat.com)
+ *
+ * This library is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
 'use strict';
 
 /* semi-convention: private functions start with lower-case letter,
@@ -371,6 +388,10 @@ EvoUndoRedo.getCommonParent = function(firstNode, secondNode)
 
        var commonParent, secondParent;
 
+       if (secondParent === document.body) {
+               return document.body;
+       }
+
        for (commonParent = firstNode.parentElement; commonParent; commonParent = commonParent.parentElement) 
{
                if (commonParent === document.body) {
                        break;
@@ -434,14 +455,16 @@ EvoUndoRedo.StartRecord = function(kind, opType, startNode, endNode)
 
                /* Tweak what to save, because some events do not modify only selection, but also its parent 
elements */
                if (kind == EvoUndoRedo.RECORD_KIND_EVENT) {
-                       if (opType == "insertLineBreak" ||
+                       if (opType == "" || // some WebKit-specific editing commands use this
+                           opType.startsWith("format") ||
+                           opType == "insertLineBreak" ||
                            opType == "insertParagraph") {
                                while (startNode && !(startNode === document.body)) {
                                        if (startNode.tagName == "P" ||
                                            startNode.tagName == "DIV" ||
                                            startNode.tagName == "BLOCKQUOTE" ||
-                                           startNode.tagName == "U" ||
-                                           startNode.tagName == "O" ||
+                                           startNode.tagName == "UL" ||
+                                           startNode.tagName == "OL" ||
                                            startNode.tagName == "PRE" ||
                                            startNode.tagName == "H1" ||
                                            startNode.tagName == "H2" ||
diff --git a/data/webkit/e-web-view.js b/data/webkit/e-web-view.js
index e84fd7eb95..c5e40c7dba 100644
--- a/data/webkit/e-web-view.js
+++ b/data/webkit/e-web-view.js
@@ -1,3 +1,20 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2019 Red Hat (www.redhat.com)
+ *
+ * This library is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
 'use strict';
 
 /* semi-convention: private functions start with lower-case letter,
diff --git a/src/modules/webkit-editor/web-extension/CMakeLists.txt 
b/src/modules/webkit-editor/web-extension/CMakeLists.txt
index ed59c7ebc7..62ac35684a 100644
--- a/src/modules/webkit-editor/web-extension/CMakeLists.txt
+++ b/src/modules/webkit-editor/web-extension/CMakeLists.txt
@@ -43,7 +43,10 @@ set(sources
        e-editor-web-extension-main.c
        e-editor-web-extension-names.h
 )
-set(extra_defines)
+set(extra_defines
+       -DEVOLUTION_WEBKITDATADIR=\"${webkitdatadir}\"
+)
+
 set(extra_cflags)
 set(extra_incdirs)
 set(extra_ldflags)
diff --git a/src/modules/webkit-editor/web-extension/e-editor-web-extension.c 
b/src/modules/webkit-editor/web-extension/e-editor-web-extension.c
index 05a1d4ac8d..73d3f4a23e 100644
--- a/src/modules/webkit-editor/web-extension/e-editor-web-extension.c
+++ b/src/modules/webkit-editor/web-extension/e-editor-web-extension.c
@@ -2600,10 +2600,79 @@ web_page_created_cb (WebKitWebExtension *wk_extension,
                extension, 0);
 }
 
+static void
+load_javascript_file (JSCContext *jsc_context,
+                     const gchar *js_filename)
+{
+       JSCValue *result;
+       JSCException *exception;
+       gchar *content, *filename, *resource_uri;
+       gsize length = 0;
+       GError *error = NULL;
+
+       g_return_if_fail (jsc_context != NULL);
+
+       filename = g_build_filename (EVOLUTION_WEBKITDATADIR, js_filename, NULL);
+
+       if (!g_file_get_contents (filename, &content, &length, &error)) {
+               g_warning ("Failed to load '%s': %s", filename, error ? error->message : "Unknown error");
+
+               g_clear_error (&error);
+               g_free (filename);
+
+               return;
+       }
+
+       resource_uri = g_strconcat ("resource:///", js_filename, NULL);
+
+       result = jsc_context_evaluate_with_source_uri (jsc_context, content, length, resource_uri, 1);
+
+       g_free (resource_uri);
+
+       exception = jsc_context_get_exception (jsc_context);
+
+       if (exception) {
+               g_warning ("Failed to call script '%s': %d:%d: %s",
+                       filename,
+                       jsc_exception_get_line_number (exception),
+                       jsc_exception_get_column_number (exception),
+                       jsc_exception_get_message (exception));
+       }
+
+       g_clear_object (&result);
+       g_free (filename);
+       g_free (content);
+}
+
+static void
+window_object_cleared_cb (WebKitScriptWorld *world,
+                         WebKitWebPage *page,
+                         WebKitFrame *frame,
+                         gpointer user_data)
+{
+       JSCContext *jsc_context;
+
+       /* Load the javascript files only to the main frame, not to the subframes */
+       if (!webkit_frame_is_main_frame (frame))
+               return;
+
+       jsc_context = webkit_frame_get_js_context (frame);
+
+       /* Read in order as each other uses the previous */
+       load_javascript_file (jsc_context, "e-convert.js");
+       load_javascript_file (jsc_context, "e-selection.js");
+       load_javascript_file (jsc_context, "e-undo-redo.js");
+       load_javascript_file (jsc_context, "e-editor.js");
+
+       g_clear_object (&jsc_context);
+}
+
 void
 e_editor_web_extension_initialize (EEditorWebExtension *extension,
                                    WebKitWebExtension *wk_extension)
 {
+       WebKitScriptWorld *script_world;
+
        g_return_if_fail (E_IS_EDITOR_WEB_EXTENSION (extension));
 
        extension->priv->wk_extension = g_object_ref (wk_extension);
@@ -2624,6 +2693,11 @@ e_editor_web_extension_initialize (EEditorWebExtension *extension,
        g_signal_connect (
                wk_extension, "page-created",
                G_CALLBACK (web_page_created_cb), extension);
+
+       script_world = webkit_script_world_get_default ();
+
+       g_signal_connect (script_world, "window-object-cleared",
+               G_CALLBACK (window_object_cleared_cb), NULL);
 }
 
 void


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]