Files
jsoncrack.com/apps/vscode/ext-src/extension.ts
T
Aykut Saraç 644ee3378f feat: migrate to turborepo and separate canvas as package (#573)
* feat: migrate to turborepo and separate canvas as package

* refactor: improve code formatting and linting scripts in react-jsoncrack package

* fix: update lint task dependencies and outputs in turbo.json

* feat: refactor project structure to use 'jsoncrack' package

* feat: rename react-jsoncrack to jsoncrack

* feat: add vscode app

* feat: enhance spinner and overlay styles for better theming support

* feat: update build scripts and fix import path for Turborepo integration

* feat: update README files to enhance project documentation and usage instructions

* fix: correct lint:fix script and update package manager version

* feat: move files to root

* feat: update version to 1.0.0 in package.json

* feat: add .npm-cache to .gitignore

* refactor: rename jsoncrack to jsoncrack-react and update imports

- Updated package.json files in apps/vscode and apps/www to replace "jsoncrack" with "jsoncrack-react" and added license information.
- Refactored imports in various components and utilities to use "jsoncrack-react" instead of "jsoncrack".
- Added ESLint and Prettier configuration files to the jsoncrack package for improved code quality.
- Adjusted code formatting across multiple files for consistency and readability.

* refactor: jsoncrack-react build with vite

* fix: update license in package.json to Apache-2.0

* refactor: remove TOML support from various components and update documentation

* chore: rename package folder to jsoncrack-react

* fix: update license field in package.json to Apache-2.0 and add LICENSE.md

* fix: react peer deps

* fix: adjust height of StyledEditorWrapper to 100% for better responsiveness

* fix: restore cursor style for jsoncrack-space in GraphView

* fix: update npm package link from `jsoncrack` to `jsoncrack-react` in README.md

* feat: add .npmrc for engine strictness and update package.json engine requirements
2026-02-19 23:16:52 +03:00

115 lines
3.3 KiB
TypeScript

import * as path from "path";
import * as vscode from "vscode";
import { createWebviewPanel } from "./webview";
function getPanelTitle(document?: vscode.TextDocument) {
if (!document) return "JSON Crack";
const fileName = path.basename(document.fileName);
return fileName || "JSON Crack";
}
export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(
vscode.commands.registerCommand("jsoncrack-vscode.start", () =>
createWebviewForActiveEditor(context)
),
vscode.commands.registerCommand("jsoncrack-vscode.start.specific", (content?: string) =>
createWebviewForContent(context, content)
),
vscode.commands.registerCommand("jsoncrack-vscode.start.selected", () =>
createWebviewForSelectedText(context)
)
);
}
// create webview for selected text
async function createWebviewForSelectedText(context: vscode.ExtensionContext) {
const editor = vscode.window.activeTextEditor;
if (editor && editor.selection.isEmpty) {
vscode.window.showInformationMessage("Please select some text first!");
return;
}
const selectedText = editor?.document.getText(editor.selection);
// Create the webview panel and send the selected JSON content
const panel = createWebviewPanel(context, getPanelTitle(editor?.document));
panel.webview.postMessage({
json: selectedText,
});
const onReceiveMessage = panel.webview.onDidReceiveMessage(e => {
if (e === "ready") {
panel.webview.postMessage({
json: selectedText,
});
}
});
const onTextChange = vscode.workspace.onDidChangeTextDocument(changeEvent => {
if (changeEvent.document === editor?.document) {
panel.webview.postMessage({
json: changeEvent.document.getText(editor?.selection),
});
}
});
const disposer = () => {
onTextChange.dispose();
onReceiveMessage.dispose();
};
panel.onDidDispose(disposer, null, context.subscriptions);
}
async function createWebviewForActiveEditor(context: vscode.ExtensionContext) {
const editor = vscode.window.activeTextEditor;
const panel = createWebviewPanel(context, getPanelTitle(editor?.document));
const onReceiveMessage = panel.webview.onDidReceiveMessage(e => {
if (e === "ready") {
panel.webview.postMessage({
json: editor?.document.getText(),
});
}
});
const onTextChange = vscode.workspace.onDidChangeTextDocument(changeEvent => {
if (changeEvent.document === editor?.document) {
panel.webview.postMessage({
json: changeEvent.document.getText(),
});
}
});
const disposer = () => {
onTextChange.dispose();
onReceiveMessage.dispose();
};
panel.onDidDispose(disposer, null, context.subscriptions);
}
/**
* Renders a readonly diagram from a string
* @param context ExtensionContext
* @param content JSON content as a string
*/
function createWebviewForContent(context?: vscode.ExtensionContext, content?: string): any {
if (context && content) {
const panel = createWebviewPanel(
context,
getPanelTitle(vscode.window.activeTextEditor?.document)
);
panel.webview.postMessage({
json: content,
});
}
}
// This method is called when your extension is deactivated
// eslint-disable-next-line @typescript-eslint/no-empty-function
export function deactivate() {}