-
-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎉 (grapher) make charts inherit indicator-level settings
- Loading branch information
1 parent
689d9e8
commit cbdb440
Showing
36 changed files
with
1,939 additions
and
829 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { | ||
isEqual, | ||
omit, | ||
GrapherInterface, | ||
diffGrapherConfigs, | ||
mergeGrapherConfigs, | ||
} from "@ourworldindata/utils" | ||
import { computed, observable, when } from "mobx" | ||
import { EditorFeatures } from "./EditorFeatures.js" | ||
import { Admin } from "./Admin.js" | ||
import { defaultGrapherConfig, Grapher } from "@ourworldindata/grapher" | ||
|
||
export type EditorTab = | ||
| "basic" | ||
| "data" | ||
| "text" | ||
| "customize" | ||
| "map" | ||
| "scatter" | ||
| "marimekko" | ||
| "revisions" | ||
| "refs" | ||
| "export" | ||
|
||
export interface AbstractChartEditorManager { | ||
admin: Admin | ||
patchConfig: GrapherInterface | ||
parentConfig?: GrapherInterface | ||
} | ||
|
||
export abstract class AbstractChartEditor< | ||
Manager extends AbstractChartEditorManager = AbstractChartEditorManager, | ||
> { | ||
manager: Manager | ||
|
||
@observable.ref grapher = new Grapher() | ||
@observable.ref currentRequest: Promise<any> | undefined // Whether the current chart state is saved or not | ||
@observable.ref tab: EditorTab = "basic" | ||
@observable.ref errorMessage?: { title: string; content: string } | ||
@observable.ref previewMode: "mobile" | "desktop" | ||
@observable.ref showStaticPreview = false | ||
@observable.ref savedPatchConfig: GrapherInterface = {} | ||
@observable.ref parentConfig: GrapherInterface | undefined = undefined | ||
|
||
constructor(props: { manager: Manager }) { | ||
this.manager = props.manager | ||
this.previewMode = | ||
localStorage.getItem("editorPreviewMode") === "mobile" | ||
? "mobile" | ||
: "desktop" | ||
|
||
when( | ||
() => this.manager.parentConfig !== undefined, | ||
() => (this.parentConfig = this.manager.parentConfig) | ||
) | ||
|
||
when( | ||
() => this.grapher.isReady, | ||
() => (this.savedPatchConfig = this.patchConfig) | ||
) | ||
} | ||
|
||
@computed get grapherConfig(): GrapherInterface { | ||
const { patchConfig, parentConfig } = this.manager | ||
if (!parentConfig) return patchConfig | ||
return mergeGrapherConfigs(parentConfig, patchConfig) | ||
} | ||
|
||
@computed get liveConfig(): GrapherInterface { | ||
return this.grapher.object | ||
} | ||
|
||
@computed get patchConfig(): GrapherInterface { | ||
const { liveConfig, parentConfig } = this | ||
if (!parentConfig) return liveConfig | ||
// TODO: explain | ||
const liveConfigWithDefaults = mergeGrapherConfigs( | ||
defaultGrapherConfig, | ||
liveConfig | ||
) | ||
return diffGrapherConfigs(liveConfigWithDefaults, parentConfig) | ||
} | ||
|
||
@computed get isModified(): boolean { | ||
return !isEqual( | ||
omit(this.patchConfig, "version"), | ||
omit(this.savedPatchConfig, "version") | ||
) | ||
} | ||
|
||
@computed get features(): EditorFeatures { | ||
return new EditorFeatures(this) | ||
} | ||
|
||
abstract get isNewGrapher(): boolean | ||
abstract get availableTabs(): EditorTab[] | ||
|
||
abstract saveGrapher(props?: { onError?: () => void }): Promise<void> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.