diff --git a/.idea/dataSources/a2c75661-f9e2-478f-a69f-6a9409e69997.xml b/.idea/dataSources/a2c75661-f9e2-478f-a69f-6a9409e69997.xml deleted file mode 100644 index d76c47f86..000000000 --- a/.idea/dataSources/a2c75661-f9e2-478f-a69f-6a9409e69997.xml +++ /dev/null @@ -1,692 +0,0 @@ - - - - - 3.16.1 - - - 1 - - - - - -
-
-
-
-
-
-
-
-
-
- 1 -
- - 1 -
- - - 1 - TEXT|0s - 1 - - - 2 - TEXT|0s - 1 - - - 3 - TEXT|0s - 1 - - - 4 - INT|0s - 1 - 0 - - - 5 - TEXT|0s - 1 - "" - - - 1 - apiTokenId - - 1 - - - apiTokenId - 1 - sqlite_autoindex_api_tokens_1 - - - 1 - TEXT|0s - 1 - - - 2 - TEXT|0s - 1 - - - 3 - TEXT|0s - 1 - - - 4 - TEXT|0s - 1 - - - 5 - TEXT|0s - 1 - '' - - - 6 - INT|0s - 1 - 0 - - - 7 - TEXT|0s - 1 - - - 8 - TEXT|0s - 1 - - - 9 - INT|0s - 1 - - - 10 - TEXT|0s - NULL - - - 11 - TEXT|0s - 1 - "" - - - 12 - int|0s - 0 - - - 1 - attributeId - - 1 - - - noteId - - - - name -value - - - - value - - - - attributeId - 1 - sqlite_autoindex_attributes_1 - - - 1 - TEXT|0s - 1 - - - 2 - TEXT|0s - 1 - - - 3 - TEXT|0s - 1 - - - 4 - INTEGER|0s - 1 - - - 5 - TEXT|0s - - - 6 - INTEGER|0s - 1 - 0 - - - 7 - INTEGER|0s - 1 - 0 - - - 8 - TEXT|0s - NULL - - - 9 - TEXT|0s - 1 - - - 10 - TEXT|0s - 1 - - - 11 - TEXT|0s - 1 - "" - - - 1 - branchId - - 1 - - - noteId -parentNoteId - - - - parentNoteId - - - - branchId - 1 - sqlite_autoindex_branches_1 - - - 1 - TEXT|0s - 1 - - - 2 - TEXT|0s - NULL - - - 3 - TEXT|0s - 1 - "" - - - 4 - TEXT|0s - 1 - - - 1 - noteId - - 1 - - - noteId - 1 - sqlite_autoindex_note_contents_1 - - - 1 - TEXT|0s - 1 - - - 2 - TEXT|0s - - - 3 - TEXT|0s - 1 - '' - - - 4 - TEXT|0s - 1 - - - 1 - noteRevisionId - - 1 - - - noteRevisionId - 1 - sqlite_autoindex_note_revision_contents_1 - - - 1 - TEXT|0s - 1 - - - 2 - TEXT|0s - 1 - - - 3 - TEXT|0s - - - 4 - INT|0s - 1 - - - 5 - INT|0s - 1 - 0 - - - 6 - INT|0s - 1 - 0 - - - 7 - TEXT|0s - 1 - - - 8 - TEXT|0s - 1 - - - 9 - TEXT|0s - 1 - - - 10 - TEXT|0s - 1 - - - 11 - TEXT|0s - 1 - - - 12 - TEXT|0s - 1 - '' - - - 13 - TEXT|0s - 1 - '' - - - 14 - TEXT|0s - 1 - '' - - - 1 - noteRevisionId - - 1 - - - noteId - - - - utcDateLastEdited - - - - utcDateCreated - - - - dateLastEdited - - - - dateCreated - - - - noteRevisionId - 1 - sqlite_autoindex_note_revisions_1 - - - 1 - TEXT|0s - 1 - - - 2 - TEXT|0s - 1 - "note" - - - 3 - INT|0s - 1 - - - 4 - INT|0s - 1 - 0 - - - 5 - TEXT|0s - 1 - 'text' - - - 6 - TEXT|0s - 1 - 'text/html' - - - 7 - TEXT|0s - 1 - "" - - - 8 - INT|0s - 1 - 0 - - - 9 - TEXT|0s - NULL - - - 10 - INT|0s - 1 - 0 - - - 11 - TEXT|0s - 1 - - - 12 - TEXT|0s - 1 - - - 13 - TEXT|0s - 1 - - - 14 - TEXT|0s - 1 - - - 1 - noteId - - 1 - - - title - - - - type - - - - isDeleted - - - - dateCreated - - - - dateModified - - - - utcDateCreated - - - - utcDateModified - - - - noteId - 1 - sqlite_autoindex_notes_1 - - - 1 - TEXT|0s - 1 - - - 2 - TEXT|0s - - - 3 - INTEGER|0s - 1 - 0 - - - 4 - TEXT|0s - 1 - "" - - - 5 - TEXT|0s - 1 - - - 6 - TEXT|0s - 1 - - - 1 - name - - 1 - - - name - 1 - sqlite_autoindex_options_1 - - - 1 - TEXT|0s - 1 - - - 2 - TEXT|0s - 1 - - - 3 - TEXT|0s - 1 - "" - - - 4 - TEXT|0s - 1 - - - 5 - INT|0s - - - 1 - noteId - - 1 - - - noteId - 1 - sqlite_autoindex_recent_notes_1 - - - 1 - TEXT|0s - 1 - - - 2 - TEXT|0s - 1 - - - 1 - sourceId - - 1 - - - utcDateCreated - - - - sourceId - 1 - sqlite_autoindex_source_ids_1 - - - 1 - text|0s - - - 2 - text|0s - - - 3 - text|0s - - - 4 - integer|0s - - - 5 - text|0s - - - 1 - - - 2 - - - 1 - INTEGER|0s - 1 - 1 - - - 2 - TEXT|0s - 1 - - - 3 - TEXT|0s - 1 - - - 4 - TEXT|0s - 1 - - - 5 - INTEGER|0s - 1 - 0 - - - 6 - TEXT|0s - 1 - - - entityName -entityId - - 1 - - - utcSyncDate - - - - id - 1 - - - \ No newline at end of file diff --git a/libraries/codemirror/mode/meta.js b/libraries/codemirror/mode/meta.js index 7ec168333..506edeb03 100644 --- a/libraries/codemirror/mode/meta.js +++ b/libraries/codemirror/mode/meta.js @@ -134,7 +134,7 @@ {name: "SPARQL", mime: "application/sparql-query", mode: "sparql", ext: ["rq", "sparql"], alias: ["sparul"]}, {name: "Spreadsheet", mime: "text/x-spreadsheet", mode: "spreadsheet", alias: ["excel", "formula"]}, {name: "SQL", mime: "text/x-sql", mode: "sql", ext: ["sql"]}, - {name: "SQLite", mime: "text/x-sqlite", mode: "sql"}, + {name: "SQLite", mimes: ["text/x-sqlite", "text/x-sqlite;schema=trilium"], mode: "sql"}, {name: "Squirrel", mime: "text/x-squirrel", mode: "clike", ext: ["nut"]}, {name: "Stylus", mime: "text/x-styl", mode: "stylus", ext: ["styl"]}, {name: "Swift", mime: "text/x-swift", mode: "swift", ext: ["swift"]}, diff --git a/src/public/app/dialogs/sql_console.js b/src/public/app/dialogs/sql_console.js deleted file mode 100644 index 39b2e6861..000000000 --- a/src/public/app/dialogs/sql_console.js +++ /dev/null @@ -1,138 +0,0 @@ -import libraryLoader from '../services/library_loader.js'; -import server from '../services/server.js'; -import toastService from "../services/toast.js"; -import utils from "../services/utils.js"; - -const $dialog = $("#sql-console-dialog"); -const $query = $('#sql-console-query'); -const $executeButton = $('#sql-console-execute'); -const $tableSchemas = $("#sql-console-table-schemas"); -const $resultContainer = $("#result-container"); - -let codeEditor; - -$dialog.on("shown.bs.modal", e => initEditor()); - -export async function showDialog() { - await showTableSchemas(); - - utils.openDialog($dialog); -} - -async function initEditor() { - if (!codeEditor) { - await libraryLoader.requireLibrary(libraryLoader.CODE_MIRROR); - - CodeMirror.keyMap.default["Shift-Tab"] = "indentLess"; - CodeMirror.keyMap.default["Tab"] = "indentMore"; - - // removing Escape binding so that Escape will propagate to the dialog (which will close on escape) - delete CodeMirror.keyMap.basic["Esc"]; - - CodeMirror.modeURL = 'libraries/codemirror/mode/%N/%N.js'; - - codeEditor = CodeMirror($query[0], { - value: "", - viewportMargin: Infinity, - indentUnit: 4, - highlightSelectionMatches: {showToken: /\w/, annotateScrollbar: false} - }); - - codeEditor.setOption("mode", "text/x-sqlite"); - CodeMirror.autoLoadMode(codeEditor, "sql"); - - codeEditor.setValue(`SELECT title, isProtected, type, mime FROM notes WHERE noteId = 'root'; ---- -SELECT noteId, parentNoteId, notePosition, prefix FROM branches WHERE branchId = 'root';`); - } - - codeEditor.focus(); -} - -async function execute() { - // execute the selected text or the whole content if there's no selection - let sqlQuery = codeEditor.getSelection(); - - if (!sqlQuery) { - sqlQuery = codeEditor.getValue(); - } - - const result = await server.post("sql/execute", { - query: sqlQuery - }); - - if (!result.success) { - toastService.showError(result.error); - return; - } - else { - toastService.showMessage("Query was executed successfully."); - } - - const results = result.results; - - $resultContainer.empty(); - - for (const rows of results) { - if (rows.length === 0) { - continue; - } - - const $table = $('
'); - $resultContainer.append($table); - - const result = rows[0]; - const $row = $(""); - - for (const key in result) { - $row.append($(""); - - for (const key in result) { - $row.append($("
").html(key)); - } - - $table.append($row); - - for (const result of rows) { - const $row = $("
").html(result[key])); - } - - $table.append($row); - } - } -} - -async function showTableSchemas() { - const tables = await server.get('sql/schema'); - - $tableSchemas.empty(); - - for (const table of tables) { - const $tableLink = $(' + + +
+ `; +let TABLE_SCHEMA; + export default class EditableCodeTypeWidget extends TypeWidget { static getType() { return "editable-code"; } doRender() { this.$widget = $(TPL); this.$editor = this.$widget.find('.note-detail-code-editor'); + this.$sqlConsoleArea = this.$widget.find('.sql-console-area'); + this.$sqlConsoleTableSchemas = this.$widget.find('.sql-console-table-schemas'); + this.$sqlConsoleExecuteButton = this.$widget.find('.sql-console-execute'); + this.$sqlConsoleResultContainer = this.$widget.find('.sql-console-result-container'); keyboardActionService.setupActionsForElement('code-detail', this.$widget, this); + utils.bindElShortcut(this.$editor, 'ctrl+return', () => this.execute()); + + this.$sqlConsoleExecuteButton.on('click', () => this.execute()); + this.initialized = this.initEditor(); return this.$widget; @@ -81,9 +135,103 @@ export default class EditableCodeTypeWidget extends TypeWidget { } }); + const isSqlConsole = note.mime === 'text/x-sqlite;schema=trilium'; + + this.$sqlConsoleArea.toggle(isSqlConsole); + + if (isSqlConsole) { + await this.showTableSchemas(); + } + this.show(); } + async showTableSchemas() { + if (!TABLE_SCHEMA) { + TABLE_SCHEMA = await server.get('sql/schema'); + } + + this.$sqlConsoleTableSchemas.empty(); + + for (const table of TABLE_SCHEMA) { + const $tableLink = $('