-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add VisualEditing component (#167)
* feat: add VisualEditing component * docs: add Visual Editing example
- Loading branch information
1 parent
7ed55c1
commit 54dbbb4
Showing
5 changed files
with
192 additions
and
40 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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 @@ | ||
export { default as VisualEditing } from "./visual-editing.astro"; |
57 changes: 57 additions & 0 deletions
57
packages/sanity-astro/src/visual-editing/visual-editing.astro
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,57 @@ | ||
--- | ||
import type { VisualEditingOptions } from '@sanity/visual-editing' | ||
export interface Props extends Pick<VisualEditingOptions, 'zIndex'> { | ||
enabled?: boolean | ||
} | ||
const {enabled, zIndex} = Astro.props | ||
--- | ||
|
||
<sanity-astro-visual-editing enabled={enabled} data-z-index={zIndex}></sanity-astro-visual-editing> | ||
|
||
<script> | ||
import {enableVisualEditing} from '@sanity/visual-editing' | ||
|
||
class SanityAstroVisualEditing extends HTMLElement { | ||
static observedAttributes = ['enabled'] | ||
private unsubscribe: (() => void) | null = null | ||
|
||
constructor() { | ||
super() | ||
} | ||
|
||
disconnectedCallback() { | ||
this.teardownVisualEditing() | ||
} | ||
|
||
attributeChangedCallback(name: string, oldValue: string | null, newValue: string | null) { | ||
if (name === 'enabled' && oldValue !== newValue) { | ||
this.updateVisualEditing(newValue === 'true') | ||
} | ||
} | ||
|
||
private updateVisualEditing(enabled: boolean) { | ||
if (enabled) { | ||
this.unsubscribe = enableVisualEditing({ | ||
zIndex: this.dataset.zIndex, | ||
refresh: () => { | ||
return new Promise((resolve) => { | ||
window.location.reload() | ||
resolve() | ||
}) | ||
}, | ||
}) | ||
} else { | ||
this.teardownVisualEditing() | ||
} | ||
} | ||
|
||
private teardownVisualEditing() { | ||
this.unsubscribe?.() | ||
this.unsubscribe = null | ||
} | ||
} | ||
|
||
customElements.define('sanity-astro-visual-editing', SanityAstroVisualEditing) | ||
</script> |