forked from graphql/vscode-graphql
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
153 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { TextDecoder, TextEncoder } from 'util'; | ||
import * as vscode from 'vscode'; | ||
|
||
export class NotebookSerializer implements vscode.NotebookSerializer { | ||
createNew(): vscode.NotebookData { | ||
const language = 'graphql'; | ||
const cell = new vscode.NotebookCellData(vscode.NotebookCellKind.Code, '', language); | ||
return new vscode.NotebookData([cell]); | ||
} | ||
|
||
serializeNotebook(data: vscode.NotebookData, token?: vscode.CancellationToken): Uint8Array { | ||
const cells = data.cells.map((cell) => { | ||
return { code: cell.value, kind: cell.kind === vscode.NotebookCellKind.Markup ? 'markdown' : 'code' }; | ||
}); | ||
return new TextEncoder().encode(JSON.stringify({ cells })); | ||
} | ||
|
||
deserializeNotebook(content: Uint8Array, token: vscode.CancellationToken): vscode.NotebookData { | ||
const stringified = content.length === 0 ? new TextDecoder().decode(this.serializeNotebook(this.createNew())) : new TextDecoder().decode(content); | ||
const data = JSON.parse(stringified); | ||
if (!('cells' in data)) { | ||
throw new Error('Unable to parse provided notebook content, missing required `cells` property.'); | ||
} | ||
if (!Array.isArray(data.cells)) { | ||
throw new Error('Unable to parse provided notebook contents, `cells` is not an array.'); | ||
} | ||
const cells: (vscode.NotebookCellData | undefined)[] = data.cells.map((cell: unknown) => { | ||
if (typeof cell !== 'object' || cell === null) { | ||
return undefined; | ||
} | ||
if (cell.hasOwnProperty('code') && cell.hasOwnProperty('kind') && 'kind' in cell) { | ||
const graphqlCell = cell as unknown as { code: string, kind: 'markdown' | 'code' }; | ||
return new vscode.NotebookCellData( | ||
graphqlCell.kind === 'code' ? vscode.NotebookCellKind.Code : vscode.NotebookCellKind.Markup, | ||
graphqlCell.code, | ||
graphqlCell.kind === 'code' ? 'graphql' : 'markdown', | ||
); | ||
} | ||
}); | ||
const cellData: vscode.NotebookCellData[] = []; | ||
for (const cell of cells) { | ||
if (cell !== undefined) { | ||
cellData.push(cell); | ||
} | ||
} | ||
return new vscode.NotebookData(cellData); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters