fix(editor): allow dropped table references
This commit is contained in:
@@ -26,6 +26,7 @@ import { extractIdentifierAt, isSqlKeyword, matchTable } from "@/lib/sqlNavigati
|
||||
import { lineColumnToOffset, parseSqlErrorLocation } from "@/lib/sqlDiagnostics";
|
||||
import {
|
||||
DBX_TABLE_REFERENCE_MIME,
|
||||
hasTableReferencePayloadType,
|
||||
parseTableReferencePayload,
|
||||
tableReferenceInsertText,
|
||||
} from "@/lib/queryEditorTableDrop";
|
||||
@@ -725,6 +726,10 @@ function droppedTableReference(event: DragEvent) {
|
||||
return parseTableReferencePayload(event.dataTransfer?.getData(DBX_TABLE_REFERENCE_MIME));
|
||||
}
|
||||
|
||||
function hasDroppedTableReference(event: DragEvent) {
|
||||
return hasTableReferencePayloadType(event.dataTransfer?.types);
|
||||
}
|
||||
|
||||
function insertDroppedTableReference(currentView: EditorViewType, event: DragEvent): boolean {
|
||||
if (props.readOnly) return false;
|
||||
const payload = droppedTableReference(event);
|
||||
@@ -1283,7 +1288,7 @@ onMounted(async () => {
|
||||
),
|
||||
EditorView.domEventHandlers({
|
||||
dragover(event) {
|
||||
if (props.readOnly || !droppedTableReference(event)) return false;
|
||||
if (props.readOnly || !hasDroppedTableReference(event)) return false;
|
||||
event.preventDefault();
|
||||
if (event.dataTransfer) event.dataTransfer.dropEffect = "copy";
|
||||
return true;
|
||||
|
||||
@@ -62,6 +62,14 @@ export function parseTableReferencePayload(value: string | undefined | null): Qu
|
||||
}
|
||||
}
|
||||
|
||||
export function hasTableReferencePayloadType(types: Iterable<string> | undefined | null): boolean {
|
||||
if (!types) return false;
|
||||
for (const type of types) {
|
||||
if (type === DBX_TABLE_REFERENCE_MIME) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export function tableReferenceInsertText(
|
||||
payload: QueryEditorTableReferencePayload,
|
||||
fallbackDatabaseType?: DatabaseType,
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { strict as assert } from "node:assert";
|
||||
import test from "node:test";
|
||||
import {
|
||||
DBX_TABLE_REFERENCE_MIME,
|
||||
createTableReferencePayload,
|
||||
hasTableReferencePayloadType,
|
||||
parseTableReferencePayload,
|
||||
serializeTableReferencePayload,
|
||||
tableReferenceInsertText,
|
||||
@@ -41,6 +43,12 @@ test("round trips table drag payload and rejects unrelated data", () => {
|
||||
assert.equal(parseTableReferencePayload(JSON.stringify({ kind: "dbx-table-reference", tableName: "orders" })), null);
|
||||
});
|
||||
|
||||
test("detects table drag payload type without reading drag data", () => {
|
||||
assert.equal(hasTableReferencePayloadType(undefined), false);
|
||||
assert.equal(hasTableReferencePayloadType(["text/plain"]), false);
|
||||
assert.equal(hasTableReferencePayloadType(["text/plain", DBX_TABLE_REFERENCE_MIME]), true);
|
||||
});
|
||||
|
||||
test("formats dropped table reference for the source database type", () => {
|
||||
assert.equal(
|
||||
tableReferenceInsertText({
|
||||
|
||||
Reference in New Issue
Block a user