Skip to content

Commit

Permalink
fix(jsonforms): add a global theme and resolve toggle text editor bug
Browse files Browse the repository at this point in the history
  • Loading branch information
peterhnm committed Jun 20, 2024
1 parent 670c441 commit 3371907
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 87 deletions.
11 changes: 8 additions & 3 deletions apps/miranum-jsonforms-preview-webview/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { vuetifyRenderers } from "@jsonforms/vue-vuetify";
import { vanillaRenderers } from "@jsonforms/vue-vanilla";
import { JsonForms } from "@jsonforms/vue";
import { boplusVueVanillaRenderers } from "@backoffice-plus/formbuilder";
import { useTheme } from "vuetify";
import VueJsonPretty from "vue-json-pretty";
import "vue-json-pretty/lib/styles.css";
Expand Down Expand Up @@ -48,20 +49,24 @@ const previewSchema = ref<JsonSchema>(defaultSchema);
const previewUiSchema = ref<UISchemaElement>(defaultUiSchema);
const renderers = ref<JsonFormsRendererRegistryEntry[]>(defaultRenderer);
const rendererStyle = ref<string>("");
const previewData = ref<any>({});
const displayData = ref<any | undefined>(undefined);
const displayErrors = ref<any | undefined>(undefined);
let loading = true;
// eslint-disable-next-line react-hooks/rules-of-hooks
const theme = useTheme();
let jsonComponentTheme = ref<"light" | "dark">("light");
onBeforeMount(async () => {
window.addEventListener("message", onReceiveMessage);
if (document.body.className.includes("vscode-dark")) {
jsonComponentTheme.value = "dark";
theme.global.name.value = "dark";
} else {
jsonComponentTheme.value = "light";
theme.global.name.value = "light";
}
vscode.postMessage(new GetJsonFormCommand());
Expand Down Expand Up @@ -91,7 +96,7 @@ async function onReceiveMessage(message: MessageEvent<Query | Command>) {
return;
}
await debouncedUpdate(jsonFormQuery.schema, jsonFormQuery.uischema);
debouncedUpdate(jsonFormQuery.schema, jsonFormQuery.uischema);
break;
}
case queryOrCommand.type === "SettingQuery": {
Expand Down
92 changes: 9 additions & 83 deletions apps/miranum-modeler/src/adapter/out/window.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import {
LogOutputChannel,
Tab,
TabInputText,
ViewColumn,
window,
workspace,
} from "vscode";
import { window } from "vscode";

import {
DisplayMessageOutPort,
LogMessageOutPort,
OpenLoggingConsoleOutPort,
TextEditorOutPort,
} from "../../application/ports/out";
import { getContext } from "@miranum-ide/vscode/miranum-vscode";
import { VsCodeLogger, VsCodeTextEditor } from "@miranum-ide/vscode/miranum-vscode";

export class VsCodeDisplayMessageAdapter implements DisplayMessageOutPort {
info(message: string): void {
Expand All @@ -26,96 +19,29 @@ export class VsCodeDisplayMessageAdapter implements DisplayMessageOutPort {
}

export class VsCodeTextEditorAdapter implements TextEditorOutPort {
private isOpen = false;

private activeDocumentPath = "";

constructor() {
const changeTab = window.tabGroups.onDidChangeTabs((tabs) => {
// Event when user closes the tab with the document
tabs.closed.forEach((tab) => {
if (
tab.input instanceof TabInputText &&
tab.input.uri.path === this.activeDocumentPath
) {
this.isOpen = false;
}
});
});

getContext().subscriptions.push(changeTab);
}
private readonly textEditor = new VsCodeTextEditor();

async toggle(documentPath: string): Promise<boolean> {
if (this.isOpen) {
this.isOpen = await this.close(documentPath);
} else {
this.isOpen = await this.open(documentPath);
}

if (this.isOpen) {
this.activeDocumentPath = documentPath;
} else {
this.activeDocumentPath = "";
}

return this.isOpen;
}

private async open(documentPath: string): Promise<boolean> {
try {
const textDocument = await workspace.openTextDocument(documentPath);
await window.showTextDocument(textDocument, ViewColumn.Beside);
return true;
} catch (error) {
return false;
}
}

private async close(documentPath: string): Promise<boolean> {
const tab = this.getTab(documentPath);

if (tab) {
return window.tabGroups.close(tab);
} else {
return false;
}
}

private getTab(documentPath: string): Tab | undefined {
for (const tabGroup of window.tabGroups.all) {
for (const tab of tabGroup.tabs) {
if (
tab.input instanceof TabInputText &&
tab.input.uri.path === documentPath
) {
return tab;
}
}
}
return undefined;
return this.textEditor.toggle(documentPath);
}
}

export class VsCodeLoggerAdapter
implements OpenLoggingConsoleOutPort, LogMessageOutPort
{
private readonly prefix = "[MiranumIDE.Modeler] ";
private readonly id = "MiranumIDE.Modeler";

private readonly logger: LogOutputChannel = window.createOutputChannel(
"MiranumIDE.Modeler",
{ log: true },
);
private readonly logger = new VsCodeLogger(this.id);

open() {
this.logger.show(true);
this.logger.open();
}

info(message: string) {
this.logger.info(this.prefix + message);
this.logger.info(message);
}

error(error: Error) {
this.logger.error(this.prefix, error);
this.logger.error(error);
}
}
2 changes: 1 addition & 1 deletion libs/vscode/miranum-vscode/src/lib/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class VsCodeTextEditor {
const tab = this.getTab(documentPath);

if (tab) {
return window.tabGroups.close(tab);
return !window.tabGroups.close(tab);
} else {
return false;
}
Expand Down

0 comments on commit 3371907

Please sign in to comment.