monaco

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 1, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Name             = "monaco"
	Version          = "0.1.0-phase0"
	RoutePrefix      = "/__gofastr/plugin/monaco"
	EditorHTMLURL    = RoutePrefix + "/editor.html"
	EditorJSURL      = RoutePrefix + "/editor.js"
	EditorCSSURL     = RoutePrefix + "/editor.css"
	AdapterScriptURL = RoutePrefix + "/adapter.js"
	// ConfigScriptURL is a tiny host-page script that publishes this plugin
	// instance's EditorConfig (set via Go options) as a global the adapter
	// merges into the manifest config it registers with the platform broker.
	// It is served non-framed (host-page) and is static per plugin instance.
	ConfigScriptURL = RoutePrefix + "/config.js"
	SaveURL         = RoutePrefix + "/save"
	DemoURL         = "/monaco"
	SchemaVersion   = "monaco-v1"
)

Identity and route constants. Both this plugin and host/adapter.js hard-code these exactly. The demo lives at /monaco (mirrors mermaid's /mermaid) so multiple plugins co-mount without colliding on "/".

Variables

View Source
var ErrConflict = errors.New("monaco: save conflict")

ErrConflict is the sentinel a WithSaveHandler hook returns to signal that the save lost an optimistic-concurrency check — the stored document changed under the editor since it loaded. handleSave maps it to HTTP 409 (E_CONFLICT) rather than the generic 500 (E_SAVE), which is the one status the host adapter relays back to the frame as a distinct saveResult so the editor can keep the doc dirty and warn the user instead of silently dropping their edits. Wrap it (fmt.Errorf("...: %w", monaco.ErrConflict)) to add context; handleSave uses errors.Is.

Functions

func DefaultCapabilities

func DefaultCapabilities() []string

DefaultCapabilities is the grant set advertised to the editor. Monaco has no upload path -- only document read/write + theme:read (same as mermaid).

func Mount

func Mount(cfg MountConfig) render.HTML

Mount renders the mount marker div plus the two hidden inputs the host adapter syncs on docChanged (code + language). It wraps the platform pluginhost.MountMarker and adds the monaco-specific data-fui-plugin-for attribute naming the code + language hidden fields. Drop it into a form. All interpolated values are HTML-escaped via render.Escape inside MountMarker.

func UIHostOption

func UIHostOption() uihost.Option

UIHostOption injects the platform broker, this plugin's config script, and this plugin's adapter (in that order — the adapter reads the config global the config script publishes, and registers with the broker the former defines).

Types

type DiffConfig

type DiffConfig struct {
	Original string `json:"original"`
	Modified string `json:"modified"`
	Language string `json:"language,omitempty"`
}

DiffConfig selects the diff-editor mount. When present (non-nil) the frame mounts monaco.editor.createDiffEditor instead of the normal editor.

type EditorConfig

type EditorConfig struct {
	Language    string      `json:"language"`
	Theme       string      `json:"theme"` // "light" | "dark" | "auto"
	ReadOnly    bool        `json:"readOnly"`
	Minimap     bool        `json:"minimap"`
	WordWrap    bool        `json:"wordWrap"`
	LineNumbers bool        `json:"lineNumbers"`
	FontSize    int         `json:"fontSize"`
	Workers     bool        `json:"workers"`
	Diff        *DiffConfig `json:"diff,omitempty"`
}

EditorConfig is the editor configuration bridged to the frame via init.config (through config.js + the adapter's manifest config). Every field is always serialized (no omitempty) so the frame always receives a complete config and never has to guess a default. The With* options above set individual slots.

type MountConfig

type MountConfig struct {
	DocID         string
	CodeField     string // hidden input name for the code text (default "code")
	LanguageField string // hidden input name for the language (default "language")
	MinHeight     string
	Doc           string // optional initial {code, language} JSON, server-rendered for reload round-trip
}

MountConfig configures Mount.

type Option

type Option func(*Plugin)

func WithCapabilities

func WithCapabilities(caps ...string) Option

WithCapabilities overrides the grant set advertised to the editor. Default: DefaultCapabilities.

func WithDemoPage

func WithDemoPage() Option

WithDemoPage registers the self-contained themed demo page at DemoURL.

func WithDevGrantAll

func WithDevGrantAll() Option

WithDevGrantAll bypasses the auth.HasScope gate on save so the demo / tests run without standing up auth. Default OFF (enforcing).

func WithDiff

func WithDiff(original, modified, language string) Option

WithDiff mounts the diff editor by default, showing original to modified. language defaults to the EditorConfig language when DiffConfig.Language is empty. WithDiff supersedes the normal editor mount for this plugin instance.

func WithEditorConfig

func WithEditorConfig(cfg EditorConfig) Option

WithEditorConfig replaces the full default EditorConfig. Use the field- specific options above for ergonomics; this is the escape hatch.

func WithFontSize

func WithFontSize(px int) Option

WithFontSize sets the editor font size in pixels.

func WithLanguage

func WithLanguage(lang string) Option

WithLanguage sets the default editor language (e.g. "go", "javascript").

func WithMinimap

func WithMinimap() Option

WithMinimap enables the minimap by default (off by default).

func WithReadOnly

func WithReadOnly() Option

WithReadOnly mounts the editor read-only by default.

func WithSaveHandler

func WithSaveHandler(fn func(ctx context.Context, req SaveRequest) error) Option

WithSaveHandler overrides the persistence hook. The default stores the canonical {code, language} doc in an in-memory map keyed by DocID.

func WithTheme

func WithTheme(theme string) Option

WithTheme sets the default theme strategy: "light", "dark", or "auto" (the frame follows the bridged host scheme). Default "auto".

func WithWordWrap

func WithWordWrap() Option

WithWordWrap enables word wrap by default (off by default).

func WithWorkers

func WithWorkers() Option

WithWorkers OPTS IN to Monaco web workers (language services: richer TS/JS completions, diagnostics, formatting). OFF by default: under the opaque- origin sandbox (sandbox="allow-scripts" WITHOUT allow-same-origin) a worker loaded from a same-origin URL or a blob:/data: URL is restricted, so the editor boots worker-free by default and degrades gracefully (the monarch tokenizer that drives syntax highlighting runs on the main thread). When opted in, the frame attempts a worker and falls back to worker-free if the sandbox refuses it.

func WithoutLineNumbers

func WithoutLineNumbers() Option

WithoutLineNumbers hides the line-number gutter (shown by default).

type Plugin

type Plugin struct {
	// contains filtered or unexported fields
}

Plugin is the Monaco code-editor plugin. It implements framework.Plugin and mirrors the mermaid plugin's shape (opaque-origin sandboxed iframe, protocol v1 over postMessage, go:embed'd frame bundle, capability gate, save handler).

func New

func New(opts ...Option) *Plugin

New constructs a Plugin. The platform manifest is built and Validate()'d here so a bad isolation/sandbox config aborts construction rather than silently de-opaquing the frame at runtime.

func (*Plugin) Capabilities

func (p *Plugin) Capabilities() []string

func (*Plugin) DefaultConfig

func (p *Plugin) DefaultConfig() EditorConfig

DefaultConfig returns the editor-config defaults this plugin instance will advertise (set via the With* options). The frame receives these through init.config and applies them on mount.

func (*Plugin) Init

func (p *Plugin) Init(app *framework.App) error

Init registers every asset and RPC route on the app's router.

func (*Plugin) LoadDoc

func (p *Plugin) LoadDoc(ctx context.Context, docID string) (docJSON string, ok bool)

LoadDoc returns the last-saved canonical {code, language} JSON for docID from the in-memory default store. ok is false when the doc has never been saved. The returned docJSON is the canonical interchange blob (schema monaco-v1).

func (*Plugin) Manifest

func (p *Plugin) Manifest() pluginhost.Manifest

func (*Plugin) Name

func (p *Plugin) Name() string

type SaveRequest

type SaveRequest struct {
	DocID         string
	Code          string
	Language      string
	SchemaVersion string
}

SaveRequest is the persistence payload handed to the save handler.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL