LoadWorkspace reads notes.json, migrating older schemas on the way in. Returns (nil, nil) when the file simply doesn't exist yet.
()
| 62 | // LoadWorkspace reads notes.json, migrating older schemas on the way in. |
| 63 | // Returns (nil, nil) when the file simply doesn't exist yet. |
| 64 | func LoadWorkspace() (*Workspace, error) { |
| 65 | path, err := DataPath() |
| 66 | if err != nil { |
| 67 | return nil, err |
| 68 | } |
| 69 | data, err := os.ReadFile(path) |
| 70 | if err != nil { |
| 71 | if errors.Is(err, os.ErrNotExist) { |
| 72 | return nil, nil |
| 73 | } |
| 74 | return nil, err |
| 75 | } |
| 76 | var f diskFile |
| 77 | if err := json.Unmarshal(data, &f); err != nil { |
| 78 | return nil, err |
| 79 | } |
| 80 | |
| 81 | // v4/v5: workspace with boards. |
| 82 | if len(f.Boards) > 0 { |
| 83 | ws := &Workspace{Boards: f.Boards, ActiveIdx: f.ActiveIdx} |
| 84 | if f.Background != nil { |
| 85 | ws.Background = *f.Background |
| 86 | ws.Background.normalizeLegacy() |
| 87 | } else { |
| 88 | // Files written before the background option: cork on, transparent. |
| 89 | ws.Background = Background{Cork: true} |
| 90 | } |
| 91 | for _, b := range ws.Boards { |
| 92 | normalizeLoadedBoard(b) |
| 93 | } |
| 94 | return ws, nil |
| 95 | } |
| 96 | |
| 97 | // v3 (or older): single board at the root. Wrap into a workspace. |
| 98 | if len(f.LegacyNotes) > 0 { |
| 99 | b := &Board{ |
| 100 | Name: "main", |
| 101 | GrainSeed: time.Now().UnixNano(), |
| 102 | Notes: f.LegacyNotes, |
| 103 | Strings: f.LegacyStrings, |
| 104 | TextMode: f.LegacyTextMode, |
| 105 | Zoom: f.LegacyZoom, |
| 106 | HighlightColor: f.LegacyHighlightColor, |
| 107 | } |
| 108 | normalizeLoadedBoard(b) |
| 109 | return &Workspace{Boards: []*Board{b}, Background: Background{Cork: true}}, nil |
| 110 | } |
| 111 | |
| 112 | // Empty file or unknown shape — let caller seed. |
| 113 | return nil, nil |
| 114 | } |
| 115 | |
| 116 | func normalizeLoadedBoard(b *Board) { |
| 117 | for _, s := range b.Strings { |