Plugin API
SyncFolds Class
The main plugin class that extends Obsidian’sPlugin base class.
Properties
Current plugin settings including the
enableSync flag and serialized fold data.Internal timer ID for the debounced save operation.
null when no save is pending.Reference to the original
localStorage.setItem method before interception.Reference to the original
localStorage.removeItem method before interception.In-memory cache of the current fold states for efficient change detection.
Public Methods
onExternalSettingsChange()
Called when the plugin’sdata.json file is modified externally (e.g., by a file sync service). This method synchronizes the external changes back to localStorage.
This method is typically called by Obsidian’s plugin system or by file sync integrations that detect changes to the plugin’s data file.
- Loads the updated settings from disk
- Compares cached fold states with newly loaded states
- Removes localStorage entries for deleted folds
- Updates localStorage entries for modified folds
- Updates the internal cache
main.ts:73-107
Lifecycle Methods
onload()
Initializes the plugin when Obsidian loads it. This is called automatically by Obsidian.- Load settings from
data.json - Check if sync is enabled
- Intercept localStorage operations
- Import or export initial fold states
- Register commands
- Initialize cache
main.ts:13-57
onunload()
Cleans up resources when the plugin is disabled or Obsidian closes.- Restore original localStorage methods
- Cancel any pending debounced saves
- Clear internal state
main.ts:109-116
Private Methods
loadSettings()
Loads plugin settings from disk.main.ts:118-124
saveSettings()
Persists current settings to disk.main.ts:126-128
getFoldsObject()
Parses the JSON-serialized fold data into aFoldStateData object.
Parsed fold state data, or an empty object if parsing fails.
main.ts:59-66
setFoldsObject()
Serializes aFoldStateData object to JSON and stores it in settings.
The fold state data to serialize and store.
main.ts:69-71
interceptLocalStorage()
InterceptslocalStorage.setItem and localStorage.removeItem to capture fold state changes.
- Wraps native localStorage methods with custom logic
- Filters for keys matching the pattern
{appId}-note-fold-{filePath} - Triggers debounced sync for matching keys
main.ts:130-159
restoreLocalStorage()
Restores the original localStorage methods, removing the plugin’s interception.main.ts:161-168
debouncedSyncFile()
Debounces fold state changes to prevent excessive disk writes.The path of the file whose fold state changed.
The new fold state as a JSON string, or
null if the fold was removed.main.ts:170-182
exportFoldsToFile()
Exports all fold states from localStorage to the plugin’s settings file.- Scans localStorage for all keys matching
{appId}-note-fold-* - Parses each fold state
- Builds a
FoldStateDataobject - Saves to
settings.folds - Updates the cache
main.ts:184-217
importFoldsToStorage()
Imports fold states from the plugin’s settings file to localStorage.- Reads
settings.folds - Parses the JSON data
- Writes each fold state to localStorage using the
{appId}-note-fold-{filePath}key pattern - Updates the cache
main.ts:219-240
upsertFoldStateForFile()
Updates or removes a single file’s fold state in the settings.The file path whose fold state should be updated.
The new fold state as JSON, or
null to remove the fold state.main.ts:242-268
Type Definitions
SyncFoldSettings
Configuration interface for the plugin.Whether fold synchronization is enabled. When
false, the plugin does not intercept localStorage or sync fold states.JSON-serialized
FoldStateData object containing all fold states.settings.ts:1-9
FoldStateData
Top-level data structure mapping file paths to their fold states.The fold properties for a specific file. The key is the file’s vault-relative path (e.g.,
"Daily Notes/2024-03-15.md").types.ts:11-13
FoldedProperties
Contains all fold information for a single file.Array of folded regions in the file. Each element represents a contiguous folded section.
Total number of lines in the document. Used by Obsidian for validation and rendering.
types.ts:6-9
Fold
Represents a single folded region in a document.Zero-indexed starting line number of the fold.
Zero-indexed ending line number of the fold (inclusive).
types.ts:1-4
Commands
The plugin registers two commands that users can invoke from the command palette.export-fold-states
Manually exports all fold states from localStorage to the plugin settings file. Command ID:export-fold-statesCommand Name: “Export folds from local storage” Usage:
- Open command palette (Ctrl/Cmd + P)
- Type “Export folds”
- Select “Sync Folds: Export folds from local storage”
- A notice confirms: “Fold states saved to settings”
- Before manually syncing the vault to another device
- To force a save of current fold states
- After recovering from a corrupted settings file
main.ts:38-45
import-fold-states
Manually imports fold states from the plugin settings file to localStorage. Command ID:import-fold-statesCommand Name: “Import folds into local storage” Usage:
- Open command palette (Ctrl/Cmd + P)
- Type “Import folds”
- Select “Sync Folds: Import folds into local storage”
- A notice confirms: “Fold states applied from settings”
- After syncing the vault from another device
- To restore fold states from a backup
- When folds appear out of sync between devices
main.ts:47-54
Usage Examples
Accessing Plugin Instance
Reading Fold States
Triggering Manual Sync
Monitoring Fold Changes
Programmatically Enabling/Disabling Sync
Logging API
The plugin includes a conditional logging system for debugging.Arguments to log. These are passed directly to
console.debug().DEBUG constant is set at build time. To enable logging:
- Modify
esbuild.config.mjsto setDEBUG: 'true' - Rebuild the plugin:
npm run build - Open the developer console in Obsidian (Ctrl/Cmd + Shift + I)
- Filter console output by
[SyncFolds]
log.ts:1-7
Version Information
Plugin ID:sync-foldsPlugin Name: Sync Folds
Current Version: 1.0.0
Minimum Obsidian Version: 0.15.0
Desktop Only: No (mobile compatible) Source Reference:
manifest.json:1-13