]> git.joonet.de Git - adminer.git/commitdiff
Plugins: add syntaxHighlighting() and CodeMirror
authorJakub Vrana <jakub@vrana.cz>
Sun, 16 Mar 2025 12:52:04 +0000 (13:52 +0100)
committerJakub Vrana <jakub@vrana.cz>
Sun, 16 Mar 2025 13:00:39 +0000 (14:00 +0100)
adminer/include/adminer.inc.php
changes.txt
editor/include/adminer.inc.php
plugins/codemirror.php [new file with mode: 0644]
plugins/plugin.php

index 197bb2e5a316d9136e8b9f85decf1fa5c3367176..24d1b09a2e1b1345a48524769b72320349828da0 100644 (file)
@@ -1018,31 +1018,7 @@ class Adminer {
                                $connection->select_db(DB);
                                $tables = table_status('', true);
                        }
-                       // this is matched by compile.php
-                       echo script_src("../externals/jush/modules/jush.js");
-                       echo script_src("../externals/jush/modules/jush-textarea.js");
-                       echo script_src("../externals/jush/modules/jush-txt.js");
-                       echo script_src("../externals/jush/modules/jush-js.js");
-                       if (support("sql")) {
-                               echo script_src("../externals/jush/modules/jush-" . JUSH . ".js");
-                               ?>
-<script<?php echo nonce(); ?>>
-<?php
-                               if ($tables) {
-                                       $links = array();
-                                       foreach ($tables as $table => $type) {
-                                               $links[] = preg_quote($table, '/');
-                                       }
-                                       echo "var jushLinks = { " . JUSH . ": [ '" . js_escape(ME) . (support("table") ? "table=" : "select=") . "\$&', /\\b(" . implode("|", $links) . ")\\b/g ] };\n";
-                                       foreach (array("bac", "bra", "sqlite_quo", "mssql_bra") as $val) {
-                                               echo "jushLinks.$val = jushLinks." . JUSH . ";\n";
-                                       }
-                               }
-                               ?>
-bodyLoad('<?php echo (is_object($connection) ? preg_replace('~^(\d\.?\d).*~s', '\1', $connection->server_info) : ""); ?>'<?php echo ($connection->maria ? ", true" : ""); ?>);
-</script>
-<?php
-                       }
+                       $this->syntaxHighlighting($tables);
                        $this->databasesPrint($missing);
                        $actions = array();
                        if (DB == "" || !$missing) {
@@ -1067,6 +1043,34 @@ bodyLoad('<?php echo (is_object($connection) ? preg_replace('~^(\d\.?\d).*~s', '
                }
        }
 
+       /** Set up syntax highlight for code and <textarea>
+       * @param array result of table_status()
+       */
+       function syntaxHighlighting($tables) {
+               global $connection;
+               // this is matched by compile.php
+               echo script_src("../externals/jush/modules/jush.js");
+               echo script_src("../externals/jush/modules/jush-textarea.js");
+               echo script_src("../externals/jush/modules/jush-txt.js");
+               echo script_src("../externals/jush/modules/jush-js.js");
+               if (support("sql")) {
+                       echo script_src("../externals/jush/modules/jush-" . JUSH . ".js");
+                       echo "<script" . nonce() . ">\n";
+                       if ($tables) {
+                               $links = array();
+                               foreach ($tables as $table => $type) {
+                                       $links[] = preg_quote($table, '/');
+                               }
+                               echo "var jushLinks = { " . JUSH . ": [ '" . js_escape(ME) . (support("table") ? "table=" : "select=") . "\$&', /\\b(" . implode("|", $links) . ")\\b/g ] };\n";
+                               foreach (array("bac", "bra", "sqlite_quo", "mssql_bra") as $val) {
+                                       echo "jushLinks.$val = jushLinks." . JUSH . ";\n";
+                               }
+                       }
+                       echo "</script>\n";
+               }
+               echo script("bodyLoad('" . (is_object($connection) ? preg_replace('~^(\d\.?\d).*~s', '\1', $connection->server_info) : "") . "'" . ($connection->maria ? ", true" : "") . ");");
+       }
+
        /** Prints databases list in menu
        * @param string
        * @return null
index a53e2dfda7172db28436b44917e3be41e921ad1c..1d8137283379f20baefe883b35468340c2e01450 100644 (file)
@@ -3,6 +3,7 @@ Align numbers right (bug #912)
 CSS: Dark mode syntax highlighting
 SQL textarea: Open help on Ctrl+click
 Designs named adminer-dark.css use dark basic style
+Plugins: add method syntaxHighlighting()
 
 Adminer 5.0.5 (released 2025-03-13):
 MySQL: Display converting function for binary, bit or geometry fields
index 6ab6da82238c1b21ed70e090470a650bc1e78cc8..508c7dae5f9d79b2ec337013aa78db598656af0a 100644 (file)
@@ -627,6 +627,9 @@ qsl('div').onclick = whisperClick;", "")
                }
        }
 
+       function syntaxHighlighting($tables) {
+       }
+
        function databasesPrint($missing) {
        }
 
diff --git a/plugins/codemirror.php b/plugins/codemirror.php
new file mode 100644 (file)
index 0000000..8d55988
--- /dev/null
@@ -0,0 +1,92 @@
+<?php
+
+/** Use Codemirror 5 for syntax highlighting and SQL <textarea> including type-ahead of keywords and tables
+* @link https://codemirror.net/5/
+* @link https://www.adminer.org/plugins/#use
+* @author Jakub Vrana, https://www.vrana.cz/
+* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
+* @license https://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 (one or other)
+*/
+class AdminerCodemirror {
+       private $root;
+
+       function __construct($root = "codemirror5") {
+               $this->root = $root;
+       }
+
+       function syntaxHighlighting($tableStatuses) {
+               $connection = Adminer\connection();
+               ?>
+<style>
+@import url(<?php echo $this->root; ?>/lib/codemirror.css);
+@import url(<?php echo $this->root; ?>/addon/hint/show-hint.css);
+.CodeMirror { border: 1px inset #ccc; resize: both; }
+</style>
+<?php
+               echo Adminer\script_src("$this->root/lib/codemirror.js");
+               echo Adminer\script_src("$this->root/addon/runmode/runmode.js");
+               echo Adminer\script_src("$this->root/addon/hint/show-hint.js");
+               echo Adminer\script_src("$this->root/mode/javascript/javascript.js");
+               if (Adminer\support("sql")) {
+                       echo Adminer\script_src("$this->root/mode/sql/sql.js");
+                       echo Adminer\script_src("$this->root/addon/hint/sql-hint.js");
+               }
+               $tables = array();
+               foreach ($tableStatuses as $status) {
+                       foreach (Adminer\fields($status["Name"]) as $name => $field) {
+                               $tables[$status["Name"]][] = $name;
+                       }
+               }
+               ?>
+<script <?php echo Adminer\nonce(); ?>>
+function getCmMode(el) {
+       const match = el.className.match(/(^|\s)jush-([^ ]+)/);
+       if (match) {
+               const modes = {
+                       js: 'application/json',
+                       sql: 'text/x-<?php echo ($connection->maria ? "mariadb" : "mysql"); ?>',
+                       oracle: 'text/x-sql',
+                       clickhouse: 'text/x-sql',
+                       firebird: 'text/x-sql'
+               };
+               return modes[match[2]] || 'text/x-' + match[2];
+       }
+}
+
+for (const el of qsa('code')) {
+       const mode = getCmMode(el);
+       if (mode) {
+               el.className += ' cm-s-default';
+               CodeMirror.runMode(el.textContent, mode, el);
+       }
+}
+
+for (const el of qsa('textarea')) {
+       const mode = getCmMode(el);
+       if (mode) {
+               const width = el.clientWidth;
+               const height = el.clientHeight;
+               const cm = CodeMirror.fromTextArea(el, {
+                       mode: mode,
+                       extraKeys: { 'Ctrl-Space': 'autocomplete' },
+                       hintOptions: {
+                               completeSingle: false,
+                               tables: <?php echo json_encode($tables); ?>,
+                               defaultTable: <?php echo json_encode($_GET["trigger"] ? $_GET["trigger"] : ($_GET["check"] ? $_GET["check"] : null)); ?>
+                       }
+               });
+               cm.setSize(width, height);
+               cm.on('inputRead', function () {
+                       const token = cm.getTokenAt(cm.getCursor());
+                       if (/^[.`"\w]\w*$/.test(token.string)) {
+                               CodeMirror.commands.autocomplete(cm);
+                       }
+               });
+               setupSubmitHighlightInput(cm.getWrapperElement());
+       }
+}
+</script>
+<?php
+               return true;
+       }
+}
index 84dcbe4c416ac4c1508ab98cf7669e998b2fd8a6..14622932af7242b9e30a77849d07bf9ab4866639 100644 (file)
@@ -407,6 +407,11 @@ class AdminerPlugin extends Adminer\Adminer {
                return $this->applyPlugin(__FUNCTION__, $args);
        }
 
+       function syntaxHighlighting($tables) {
+               $args = func_get_args();
+               return $this->applyPlugin(__FUNCTION__, $args);
+       }
+
        function databasesPrint($missing) {
                $args = func_get_args();
                return $this->applyPlugin(__FUNCTION__, $args);