2024-05-29 00:41:03 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2024-08-20 23:56:48 +02:00
|
|
|
package waveobj
|
2024-05-29 00:41:03 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2024-06-12 02:42:10 +02:00
|
|
|
"fmt"
|
2024-05-29 00:41:03 +02:00
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
2024-08-20 23:56:48 +02:00
|
|
|
type UpdatesRtnType = []WaveObjUpdate
|
|
|
|
|
2024-05-29 00:41:03 +02:00
|
|
|
type UIContext struct {
|
|
|
|
WindowId string `json:"windowid"`
|
|
|
|
ActiveTabId string `json:"activetabid"`
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
UpdateType_Update = "update"
|
|
|
|
UpdateType_Delete = "delete"
|
|
|
|
)
|
|
|
|
|
2024-06-06 02:21:40 +02:00
|
|
|
const (
|
2024-08-15 03:40:41 +02:00
|
|
|
OType_Client = "client"
|
|
|
|
OType_Window = "window"
|
|
|
|
OType_Workspace = "workspace"
|
|
|
|
OType_Tab = "tab"
|
|
|
|
OType_LayoutState = "layout"
|
|
|
|
OType_Block = "block"
|
2024-06-06 02:21:40 +02:00
|
|
|
)
|
|
|
|
|
2024-05-29 00:41:03 +02:00
|
|
|
type WaveObjUpdate struct {
|
2024-08-20 23:56:48 +02:00
|
|
|
UpdateType string `json:"updatetype"`
|
|
|
|
OType string `json:"otype"`
|
|
|
|
OID string `json:"oid"`
|
|
|
|
Obj WaveObj `json:"obj,omitempty"`
|
2024-05-29 00:41:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (update WaveObjUpdate) MarshalJSON() ([]byte, error) {
|
|
|
|
rtn := make(map[string]any)
|
|
|
|
rtn["updatetype"] = update.UpdateType
|
|
|
|
rtn["otype"] = update.OType
|
|
|
|
rtn["oid"] = update.OID
|
|
|
|
if update.Obj != nil {
|
|
|
|
var err error
|
2024-08-20 23:56:48 +02:00
|
|
|
rtn["obj"], err = ToJsonMap(update.Obj)
|
2024-05-29 00:41:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return json.Marshal(rtn)
|
|
|
|
}
|
|
|
|
|
2024-08-20 23:56:48 +02:00
|
|
|
func MakeUpdate(obj WaveObj) WaveObjUpdate {
|
2024-06-12 02:42:10 +02:00
|
|
|
return WaveObjUpdate{
|
|
|
|
UpdateType: UpdateType_Update,
|
|
|
|
OType: obj.GetOType(),
|
2024-08-20 23:56:48 +02:00
|
|
|
OID: GetOID(obj),
|
2024-06-12 02:42:10 +02:00
|
|
|
Obj: obj,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-20 23:56:48 +02:00
|
|
|
func MakeUpdates(objs []WaveObj) []WaveObjUpdate {
|
2024-06-12 02:42:10 +02:00
|
|
|
rtn := make([]WaveObjUpdate, 0, len(objs))
|
|
|
|
for _, obj := range objs {
|
|
|
|
rtn = append(rtn, MakeUpdate(obj))
|
|
|
|
}
|
|
|
|
return rtn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (update *WaveObjUpdate) UnmarshalJSON(data []byte) error {
|
|
|
|
var objMap map[string]any
|
|
|
|
err := json.Unmarshal(data, &objMap)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var ok1, ok2, ok3 bool
|
|
|
|
if _, found := objMap["updatetype"]; !found {
|
|
|
|
return fmt.Errorf("missing updatetype (in WaveObjUpdate)")
|
|
|
|
}
|
|
|
|
update.UpdateType, ok1 = objMap["updatetype"].(string)
|
|
|
|
if !ok1 {
|
|
|
|
return fmt.Errorf("in WaveObjUpdate bad updatetype type %T", objMap["updatetype"])
|
|
|
|
}
|
|
|
|
if _, found := objMap["otype"]; !found {
|
|
|
|
return fmt.Errorf("missing otype (in WaveObjUpdate)")
|
|
|
|
}
|
|
|
|
update.OType, ok2 = objMap["otype"].(string)
|
|
|
|
if !ok2 {
|
|
|
|
return fmt.Errorf("in WaveObjUpdate bad otype type %T", objMap["otype"])
|
|
|
|
}
|
|
|
|
if _, found := objMap["oid"]; !found {
|
|
|
|
return fmt.Errorf("missing oid (in WaveObjUpdate)")
|
|
|
|
}
|
|
|
|
update.OID, ok3 = objMap["oid"].(string)
|
|
|
|
if !ok3 {
|
|
|
|
return fmt.Errorf("in WaveObjUpdate bad oid type %T", objMap["oid"])
|
|
|
|
}
|
|
|
|
if _, found := objMap["obj"]; found {
|
|
|
|
objMap, ok := objMap["obj"].(map[string]any)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("in WaveObjUpdate bad obj type %T", objMap["obj"])
|
|
|
|
}
|
2024-08-20 23:56:48 +02:00
|
|
|
waveObj, err := FromJsonMap(objMap)
|
2024-06-12 02:42:10 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("in WaveObjUpdate error decoding obj: %w", err)
|
|
|
|
}
|
|
|
|
update.Obj = waveObj
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-29 00:41:03 +02:00
|
|
|
type Client struct {
|
2024-09-26 00:52:12 +02:00
|
|
|
OID string `json:"oid"`
|
|
|
|
Version int `json:"version"`
|
|
|
|
WindowIds []string `json:"windowids"`
|
|
|
|
Meta MetaMapType `json:"meta"`
|
|
|
|
TosAgreed int64 `json:"tosagreed,omitempty"`
|
|
|
|
HasOldHistory bool `json:"hasoldhistory,omitempty"`
|
2024-05-29 00:41:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*Client) GetOType() string {
|
2024-06-06 02:21:40 +02:00
|
|
|
return OType_Client
|
2024-05-29 00:41:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// stores the ui-context of the window
|
|
|
|
// workspaceid, active tab, active block within each tab, window size, etc.
|
|
|
|
type Window struct {
|
2024-08-26 20:56:00 +02:00
|
|
|
OID string `json:"oid"`
|
|
|
|
Version int `json:"version"`
|
|
|
|
WorkspaceId string `json:"workspaceid"`
|
|
|
|
ActiveTabId string `json:"activetabid"`
|
2024-10-11 01:12:56 +02:00
|
|
|
IsNew bool `json:"isnew,omitempty"` // set when a window is created on the backend so the FE can size it properly. cleared on first resize
|
2024-08-26 20:56:00 +02:00
|
|
|
Pos Point `json:"pos"`
|
|
|
|
WinSize WinSize `json:"winsize"`
|
|
|
|
LastFocusTs int64 `json:"lastfocusts"`
|
|
|
|
Meta MetaMapType `json:"meta"`
|
2024-05-29 00:41:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*Window) GetOType() string {
|
2024-06-06 02:21:40 +02:00
|
|
|
return OType_Window
|
2024-05-29 00:41:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Workspace struct {
|
2024-07-30 21:33:28 +02:00
|
|
|
OID string `json:"oid"`
|
|
|
|
Version int `json:"version"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
TabIds []string `json:"tabids"`
|
|
|
|
Meta MetaMapType `json:"meta"`
|
2024-05-29 00:41:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*Workspace) GetOType() string {
|
2024-06-06 02:21:40 +02:00
|
|
|
return OType_Workspace
|
2024-05-29 00:41:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Tab struct {
|
2024-08-15 03:40:41 +02:00
|
|
|
OID string `json:"oid"`
|
|
|
|
Version int `json:"version"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
LayoutState string `json:"layoutstate"`
|
|
|
|
BlockIds []string `json:"blockids"`
|
|
|
|
Meta MetaMapType `json:"meta"`
|
2024-05-29 00:41:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*Tab) GetOType() string {
|
2024-06-06 02:21:40 +02:00
|
|
|
return OType_Tab
|
|
|
|
}
|
|
|
|
|
2024-08-20 23:56:48 +02:00
|
|
|
func (t *Tab) GetBlockORefs() []ORef {
|
|
|
|
rtn := make([]ORef, 0, len(t.BlockIds))
|
2024-06-12 02:42:10 +02:00
|
|
|
for _, blockId := range t.BlockIds {
|
2024-08-20 23:56:48 +02:00
|
|
|
rtn = append(rtn, ORef{OType: OType_Block, OID: blockId})
|
2024-06-12 02:42:10 +02:00
|
|
|
}
|
|
|
|
return rtn
|
|
|
|
}
|
|
|
|
|
2024-08-27 00:17:37 +02:00
|
|
|
type LayoutActionData struct {
|
|
|
|
ActionType string `json:"actiontype"`
|
|
|
|
BlockId string `json:"blockid"`
|
2024-08-28 03:38:57 +02:00
|
|
|
NodeSize *uint `json:"nodesize,omitempty"`
|
|
|
|
IndexArr *[]int `json:"indexarr,omitempty"`
|
2024-08-28 08:16:07 +02:00
|
|
|
Focused bool `json:"focused"`
|
|
|
|
Magnified bool `json:"magnified"`
|
2024-08-27 00:17:37 +02:00
|
|
|
}
|
|
|
|
|
2024-08-31 05:20:25 +02:00
|
|
|
type LeafOrderEntry struct {
|
|
|
|
NodeId string `json:"nodeid"`
|
|
|
|
BlockId string `json:"blockid"`
|
|
|
|
}
|
|
|
|
|
2024-08-15 03:40:41 +02:00
|
|
|
type LayoutState struct {
|
2024-08-28 03:38:57 +02:00
|
|
|
OID string `json:"oid"`
|
|
|
|
Version int `json:"version"`
|
|
|
|
RootNode any `json:"rootnode,omitempty"`
|
|
|
|
MagnifiedNodeId string `json:"magnifiednodeid,omitempty"`
|
|
|
|
FocusedNodeId string `json:"focusednodeid,omitempty"`
|
2024-08-31 05:20:25 +02:00
|
|
|
LeafOrder *[]LeafOrderEntry `json:"leaforder,omitempty"`
|
2024-08-28 03:38:57 +02:00
|
|
|
PendingBackendActions *[]LayoutActionData `json:"pendingbackendactions,omitempty"`
|
|
|
|
Meta MetaMapType `json:"meta,omitempty"`
|
2024-06-06 02:21:40 +02:00
|
|
|
}
|
|
|
|
|
2024-08-15 03:40:41 +02:00
|
|
|
func (*LayoutState) GetOType() string {
|
|
|
|
return OType_LayoutState
|
2024-05-29 00:41:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type FileDef struct {
|
|
|
|
FileType string `json:"filetype,omitempty"`
|
|
|
|
Path string `json:"path,omitempty"`
|
|
|
|
Url string `json:"url,omitempty"`
|
|
|
|
Content string `json:"content,omitempty"`
|
|
|
|
Meta map[string]any `json:"meta,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type BlockDef struct {
|
2024-07-30 21:33:28 +02:00
|
|
|
Files map[string]*FileDef `json:"files,omitempty"`
|
|
|
|
Meta MetaMapType `json:"meta,omitempty"`
|
2024-05-29 00:41:03 +02:00
|
|
|
}
|
|
|
|
|
2024-06-18 07:38:48 +02:00
|
|
|
type StickerClickOptsType struct {
|
|
|
|
SendInput string `json:"sendinput,omitempty"`
|
|
|
|
CreateBlock *BlockDef `json:"createblock,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type StickerDisplayOptsType struct {
|
|
|
|
Icon string `json:"icon"`
|
|
|
|
ImgSrc string `json:"imgsrc"`
|
|
|
|
SvgBlob string `json:"svgblob,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type StickerType struct {
|
|
|
|
StickerType string `json:"stickertype"`
|
|
|
|
Style map[string]any `json:"style"`
|
|
|
|
ClickOpts *StickerClickOptsType `json:"clickopts,omitempty"`
|
|
|
|
Display *StickerDisplayOptsType `json:"display"`
|
|
|
|
}
|
|
|
|
|
2024-05-29 00:41:03 +02:00
|
|
|
type RuntimeOpts struct {
|
2024-08-17 20:21:25 +02:00
|
|
|
TermSize TermSize `json:"termsize,omitempty"`
|
|
|
|
WinSize WinSize `json:"winsize,omitempty"`
|
2024-05-29 00:41:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Point struct {
|
|
|
|
X int `json:"x"`
|
|
|
|
Y int `json:"y"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type WinSize struct {
|
|
|
|
Width int `json:"width"`
|
|
|
|
Height int `json:"height"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Block struct {
|
|
|
|
OID string `json:"oid"`
|
|
|
|
Version int `json:"version"`
|
|
|
|
BlockDef *BlockDef `json:"blockdef"`
|
|
|
|
RuntimeOpts *RuntimeOpts `json:"runtimeopts,omitempty"`
|
2024-06-18 07:38:48 +02:00
|
|
|
Stickers []*StickerType `json:"stickers,omitempty"`
|
2024-07-30 21:33:28 +02:00
|
|
|
Meta MetaMapType `json:"meta"`
|
2024-05-29 00:41:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*Block) GetOType() string {
|
2024-06-06 02:21:40 +02:00
|
|
|
return OType_Block
|
2024-05-29 00:41:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func AllWaveObjTypes() []reflect.Type {
|
|
|
|
return []reflect.Type{
|
|
|
|
reflect.TypeOf(&Client{}),
|
|
|
|
reflect.TypeOf(&Window{}),
|
|
|
|
reflect.TypeOf(&Workspace{}),
|
|
|
|
reflect.TypeOf(&Tab{}),
|
|
|
|
reflect.TypeOf(&Block{}),
|
2024-08-15 03:40:41 +02:00
|
|
|
reflect.TypeOf(&LayoutState{}),
|
2024-05-29 00:41:03 +02:00
|
|
|
}
|
|
|
|
}
|
2024-08-17 20:21:25 +02:00
|
|
|
|
|
|
|
type TermSize struct {
|
|
|
|
Rows int `json:"rows"`
|
|
|
|
Cols int `json:"cols"`
|
|
|
|
}
|