init
This commit is contained in:
53
src/ApplyView.tsx
Normal file
53
src/ApplyView.tsx
Normal file
@@ -0,0 +1,53 @@
|
||||
import { TFile, View, WorkspaceLeaf } from 'obsidian'
|
||||
import { Root, createRoot } from 'react-dom/client'
|
||||
|
||||
import ApplyViewRoot from './components/apply-view/ApplyViewRoot'
|
||||
import { APPLY_VIEW_TYPE } from './constants'
|
||||
import { AppProvider } from './contexts/AppContext'
|
||||
|
||||
export type ApplyViewState = {
|
||||
file: TFile
|
||||
originalContent: string
|
||||
newContent: string
|
||||
}
|
||||
|
||||
export class ApplyView extends View {
|
||||
private root: Root | null = null
|
||||
|
||||
private state: ApplyViewState | null = null
|
||||
|
||||
constructor(leaf: WorkspaceLeaf) {
|
||||
super(leaf)
|
||||
}
|
||||
|
||||
getViewType() {
|
||||
return APPLY_VIEW_TYPE
|
||||
}
|
||||
|
||||
getDisplayText() {
|
||||
return `Applying: ${this.state?.file?.name ?? ''}`
|
||||
}
|
||||
|
||||
async setState(state: ApplyViewState) {
|
||||
this.state = state
|
||||
// Should render here because onOpen is called before setState
|
||||
this.render()
|
||||
}
|
||||
|
||||
async onOpen() {
|
||||
this.root = createRoot(this.containerEl)
|
||||
}
|
||||
|
||||
async onClose() {
|
||||
this.root?.unmount()
|
||||
}
|
||||
|
||||
async render() {
|
||||
if (!this.root || !this.state) return
|
||||
this.root.render(
|
||||
<AppProvider app={this.app}>
|
||||
<ApplyViewRoot state={this.state} close={() => this.leaf.detach()} />
|
||||
</AppProvider>,
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user