Skip to content

Commit

Permalink
feat: add support for creating custom editors for files
Browse files Browse the repository at this point in the history
  • Loading branch information
CompuIves committed Nov 7, 2023
1 parent 29f389a commit 7be06d1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
2 changes: 2 additions & 0 deletions demo/src/features/customView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ const { CustomEditorInput } = registerEditorPane({

return {
dispose () {
},
setInput(input: EditorInput) {
}
}
}
Expand Down
32 changes: 26 additions & 6 deletions src/service-override/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ import { IContextViewService } from 'vs/platform/contextview/browser/contextView
import { ContextViewService } from 'vs/platform/contextview/browser/contextViewService'
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService'
import { EditorInput, IEditorCloseHandler } from 'vs/workbench/common/editor/editorInput'
import { EditorExtensions, Verbosity } from 'vs/workbench/common/editor'
import { IEditorOptions } from 'vs/platform/editor/common/editor'
import { EditorExtensions, IEditorOpenContext, Verbosity } from 'vs/workbench/common/editor'
import { IEditorOptions, IResourceEditorInput, ITextResourceEditorInput } from 'vs/platform/editor/common/editor'
import { IResolvedTextEditorModel } from 'vs/editor/common/services/resolverService'
import { ITextEditorService, TextEditorService } from 'vs/workbench/services/textfile/common/textEditorService'
import { CodeEditorService } from 'vs/workbench/services/editor/browser/codeEditorService'
Expand Down Expand Up @@ -96,6 +96,7 @@ import getKeybindingsOverride from './keybindings'
import { changeUrlDomain } from './tools/url'
import { registerAssets } from '../assets'
import { registerServiceInitializePostParticipant } from '../lifecycle'
import { CancellationToken } from 'vs/base/common/cancellation'

Check warning on line 99 in src/service-override/views.ts

View workflow job for this annotation

GitHub Actions / Check build

`vs/base/common/cancellation` import should occur before import of `./tools/editor`

function createPart (id: string, role: string, classes: string[]): HTMLElement {
const part = document.createElement(role === 'status' ? 'footer' /* Use footer element for status bar #98376 */ : 'div')
Expand Down Expand Up @@ -190,6 +191,10 @@ function renderStatusBarPart (container: HTMLElement): IDisposable {
return attachPart(Parts.STATUSBAR_PART, container)
}

interface BodyRenderer extends IDisposable {
setInput? (input: EditorInput, options: IEditorOptions | undefined, context: IEditorOpenContext, token: CancellationToken): Promise<void>
}

type Label = string | {
short: string
medium: string
Expand All @@ -198,7 +203,7 @@ type Label = string | {
interface EditorPanelOption {
readonly id: string
name: string
renderBody (container: HTMLElement): IDisposable
renderBody (container: HTMLElement): BodyRenderer
}

interface SimpleEditorInput extends EditorInput {
Expand All @@ -211,6 +216,7 @@ interface SimpleEditorInput extends EditorInput {
function registerEditorPane (options: EditorPanelOption): { disposable: IDisposable, CustomEditorInput: new (closeHandler?: IEditorCloseHandler) => SimpleEditorInput } {
class CustomEditorPane extends EditorPane {
private content?: HTMLElement
private bodyRenderer?: BodyRenderer
constructor (
@ITelemetryService telemetryService: ITelemetryService,
@IThemeService themeService: IThemeService,
Expand All @@ -224,13 +230,22 @@ function registerEditorPane (options: EditorPanelOption): { disposable: IDisposa
this.content.style.display = 'flex'
this.content.style.alignItems = 'stretch'
append(parent, this.content)
this._register(options.renderBody(this.content))
this.bodyRenderer = options.renderBody(this.content)
this._register(this.bodyRenderer)
}

override layout (dimension: Dimension): void {
this.content!.style.height = `${dimension.height}px`
this.content!.style.width = `${dimension.width}px`
}

override async setInput (input: EditorInput, options: IEditorOptions | undefined, context: IEditorOpenContext, token: CancellationToken): Promise<void> {
if (this.bodyRenderer != null && this.bodyRenderer.setInput != null) {
await this.bodyRenderer.setInput(input, options, context, token)
}

return super.setInput(input, options, context, token)
}
}

class CustomEditorInput extends EditorInput implements SimpleEditorInput {
Expand All @@ -241,7 +256,7 @@ function registerEditorPane (options: EditorPanelOption): { disposable: IDisposa
private description: Label = options.name
private dirty: boolean = false

constructor (public override readonly closeHandler?: IEditorCloseHandler) {
constructor (public override readonly closeHandler?: IEditorCloseHandler, public baseInput?: IResourceEditorInput | ITextResourceEditorInput | undefined, private internalEditorId?: string | undefined) {
super()
}

Expand All @@ -250,6 +265,9 @@ function registerEditorPane (options: EditorPanelOption): { disposable: IDisposa
}

override get resource (): URI | undefined {
if (this.baseInput != null) {
return this.baseInput.resource
}
return undefined
}

Expand Down Expand Up @@ -681,5 +699,7 @@ export {
SidebarPart,
ActivitybarPart,
PanelPart,
Parts
Parts,

BodyRenderer
}

0 comments on commit 7be06d1

Please sign in to comment.