|
1 | | -// Copyright 2025, Command Line Inc. |
| 1 | +// Copyright 2026, Command Line Inc. |
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
4 | 4 | package vdom |
5 | 5 |
|
| 6 | +import ( |
| 7 | + "encoding/json" |
| 8 | + "sync/atomic" |
| 9 | +) |
| 10 | + |
6 | 11 | const TextTag = "#text" |
7 | 12 | const WaveTextTag = "wave:text" |
8 | 13 | const WaveNullTag = "wave:null" |
@@ -36,8 +41,41 @@ type VDomRef struct { |
36 | 41 | Type string `json:"type" tstype:"\"ref\""` |
37 | 42 | RefId string `json:"refid"` |
38 | 43 | TrackPosition bool `json:"trackposition,omitempty"` |
39 | | - Position *VDomRefPosition `json:"position,omitempty"` |
40 | | - HasCurrent bool `json:"hascurrent,omitempty"` |
| 44 | + Position *VDomRefPosition `json:"-"` |
| 45 | + HasCurrent atomic.Bool `json:"-"` |
| 46 | +} |
| 47 | + |
| 48 | +func (r *VDomRef) MarshalJSON() ([]byte, error) { |
| 49 | + type vdomRefAlias struct { |
| 50 | + Type string `json:"type"` |
| 51 | + RefId string `json:"refid"` |
| 52 | + TrackPosition bool `json:"trackposition,omitempty"` |
| 53 | + HasCurrent bool `json:"hascurrent,omitempty"` |
| 54 | + } |
| 55 | + return json.Marshal(vdomRefAlias{ |
| 56 | + Type: r.Type, |
| 57 | + RefId: r.RefId, |
| 58 | + TrackPosition: r.TrackPosition, |
| 59 | + HasCurrent: r.HasCurrent.Load(), |
| 60 | + }) |
| 61 | +} |
| 62 | + |
| 63 | +func (r *VDomRef) UnmarshalJSON(data []byte) error { |
| 64 | + type vdomRefAlias struct { |
| 65 | + Type string `json:"type"` |
| 66 | + RefId string `json:"refid"` |
| 67 | + TrackPosition bool `json:"trackposition,omitempty"` |
| 68 | + HasCurrent bool `json:"hascurrent,omitempty"` |
| 69 | + } |
| 70 | + var alias vdomRefAlias |
| 71 | + if err := json.Unmarshal(data, &alias); err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + r.Type = alias.Type |
| 75 | + r.RefId = alias.RefId |
| 76 | + r.TrackPosition = alias.TrackPosition |
| 77 | + r.HasCurrent.Store(alias.HasCurrent) |
| 78 | + return nil |
41 | 79 | } |
42 | 80 |
|
43 | 81 | type VDomSimpleRef[T any] struct { |
@@ -66,14 +104,14 @@ type VDomEvent struct { |
66 | 104 | WaveId string `json:"waveid"` |
67 | 105 | EventType string `json:"eventtype"` // usually the prop name (e.g. onClick, onKeyDown) |
68 | 106 | GlobalEventType string `json:"globaleventtype,omitempty"` |
69 | | - TargetValue string `json:"targetvalue,omitempty"` // set for onChange events on input/textarea/select |
| 107 | + TargetValue string `json:"targetvalue,omitempty"` // set for onChange events on input/textarea/select |
70 | 108 | TargetChecked bool `json:"targetchecked,omitempty"` // set for onChange events on checkbox/radio inputs |
71 | | - TargetName string `json:"targetname,omitempty"` // target element's name attribute |
72 | | - TargetId string `json:"targetid,omitempty"` // target element's id attribute |
73 | | - TargetFiles []VDomFileData `json:"targetfiles,omitempty"` // set for onChange events on file inputs |
74 | | - KeyData *VDomKeyboardEvent `json:"keydata,omitempty"` // set for onKeyDown events |
75 | | - MouseData *VDomPointerData `json:"mousedata,omitempty"` // set for onClick, onMouseDown, onMouseUp, onDoubleClick events |
76 | | - FormData *VDomFormData `json:"formdata,omitempty"` // set for onSubmit events on forms |
| 109 | + TargetName string `json:"targetname,omitempty"` // target element's name attribute |
| 110 | + TargetId string `json:"targetid,omitempty"` // target element's id attribute |
| 111 | + TargetFiles []VDomFileData `json:"targetfiles,omitempty"` // set for onChange events on file inputs |
| 112 | + KeyData *VDomKeyboardEvent `json:"keydata,omitempty"` // set for onKeyDown events |
| 113 | + MouseData *VDomPointerData `json:"mousedata,omitempty"` // set for onClick, onMouseDown, onMouseUp, onDoubleClick events |
| 114 | + FormData *VDomFormData `json:"formdata,omitempty"` // set for onSubmit events on forms |
77 | 115 | } |
78 | 116 |
|
79 | 117 | type VDomKeyboardEvent struct { |
@@ -115,13 +153,13 @@ type VDomPointerData struct { |
115 | 153 | } |
116 | 154 |
|
117 | 155 | type VDomFormData struct { |
118 | | - Action string `json:"action,omitempty"` |
119 | | - Method string `json:"method"` |
120 | | - Enctype string `json:"enctype"` |
121 | | - FormId string `json:"formid,omitempty"` |
122 | | - FormName string `json:"formname,omitempty"` |
123 | | - Fields map[string][]string `json:"fields"` |
124 | | - Files map[string][]VDomFileData `json:"files"` |
| 156 | + Action string `json:"action,omitempty"` |
| 157 | + Method string `json:"method"` |
| 158 | + Enctype string `json:"enctype"` |
| 159 | + FormId string `json:"formid,omitempty"` |
| 160 | + FormName string `json:"formname,omitempty"` |
| 161 | + Fields map[string][]string `json:"fields"` |
| 162 | + Files map[string][]VDomFileData `json:"files"` |
125 | 163 | } |
126 | 164 |
|
127 | 165 | func (f *VDomFormData) GetField(fieldName string) string { |
|
0 commit comments