1
0
mirror of https://github.com/vrana/adminer.git synced 2025-08-06 06:37:33 +02:00

Plugins: add syntaxHighlighting() and CodeMirror

This commit is contained in:
Jakub Vrana
2025-03-16 13:52:04 +01:00
parent 4fee062b73
commit 517f63835d
5 changed files with 130 additions and 25 deletions

View 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

View 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

View File

@@ -627,6 +627,9 @@ qsl('div').onclick = whisperClick;", "")
}
}
function syntaxHighlighting($tables) {
}
function databasesPrint($missing) {
}

92
plugins/codemirror.php Normal file
View File

@@ -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;
}
}

View 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);