Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

svg export via cli command and headless browser #439

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@
"name": "Vitest: Run All",
"type": "node",
"request": "launch",
"skipFiles": ["<node_internals>/**", "**/node_modules/**"],
"skipFiles": [
"<node_internals>/**",
"**/node_modules/**"
],
"program": "${workspaceRoot}/node_modules/vitest/vitest.mjs",
"args": ["run", "--no-color", "--no-coverage", "--no-watch"],
"args": [
"run",
"--no-color",
"--no-coverage",
"--no-watch"
],
"smartStep": true,
"console": "integratedTerminal",
},
Expand All @@ -16,9 +24,15 @@
"type": "node",
"request": "launch",
"autoAttachChildProcesses": true,
"skipFiles": ["<node_internals>/**", "**/node_modules/**"],
"skipFiles": [
"<node_internals>/**",
"**/node_modules/**"
],
"program": "${workspaceRoot}/node_modules/vitest/vitest.mjs",
"args": ["run", "${relativeFile}"],
"args": [
"run",
"${relativeFile}"
],
"smartStep": true,
"console": "integratedTerminal",
},
Expand All @@ -41,6 +55,17 @@
"${workspaceFolder}/packages/sprotty-protocol/lib/**/*.js",
"${workspaceFolder}/packages/sprotty-elk/lib/**/*.js"
]
},
{
"type": "node",
"request": "launch",
"name": "Trigger export command",
"runtimeExecutable": "${workspaceFolder}/packages/sprotty/bin/sprotty",
"args": [
"export",
"http://localhost:8080/jsxample/jsxample.html"
],
"cwd": "${workspaceFolder}"
}
]
}
6 changes: 5 additions & 1 deletion examples/jsxample/src/standalone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { LocalModelSource, TYPES } from 'sprotty';
import { LocalModelSource, RequestExportSvgAction, TYPES } from 'sprotty';
import createContainer from './di.config';

export default async function runJsxample() {
const container = createContainer('sprotty');
const modelSource = container.get<LocalModelSource>(TYPES.ModelSource);

document.addEventListener('export', () => {
modelSource.actionDispatcher.dispatch(RequestExportSvgAction.create());
});

const graph = {
id: 'root',
type: 'graph',
Expand Down
2 changes: 2 additions & 0 deletions packages/sprotty/bin/sprotty
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
require('../lib/cli/sprotty')
6 changes: 6 additions & 0 deletions packages/sprotty/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
},
"dependencies": {
"autocompleter": "^9.1.2",
"commander": "^12.0.0",
"file-saver": "^2.0.5",
"inversify": "~6.0.2",
"puppeteer": "^22.2.0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not have these dependencies in the base package of Sprotty. If we go for this solution, the new code should be moved to a new package.

"snabbdom": "~3.5.1",
"sprotty-protocol": "^1.1.0",
"tinyqueue": "^2.0.3"
Expand All @@ -45,10 +47,14 @@
"test": "vitest run --config ../../vite.config.ts"
},
"files": [
"bin",
"lib",
"css",
"src"
],
"bin": {
"sprotty": "bin/sprotty"
},
"main": "lib/index",
"types": "lib/index"
}
42 changes: 42 additions & 0 deletions packages/sprotty/src/cli/sprotty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/********************************************************************************
* Copyright (c) 2024 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { Command } from 'commander';
import puppeteer from 'puppeteer';

export function sprottyCli(argv: string[]): void {
const program = new Command();

program.command('export')
.argument('<url>', 'The url of the Sprotty page')
.action(async (url: string) => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
await page.evaluate(() => {
// trigger the export
document.dispatchEvent(new Event('export'));
});

// await browser.close();
});

program.parse(argv);

}

// run the command
sprottyCli(process.argv);
Loading
Loading