generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from mailcarrierapp/feat/live-preview
[2.x] Live preview
- Loading branch information
Showing
30 changed files
with
664 additions
and
55 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
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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"/js/highlight.js": "/js/highlight.js", | ||
"/js/codemirror.component.js": "/js/codemirror.component.js", | ||
"/css/theme.css": "/css/theme.css", | ||
"/css/highlight.css": "/css/highlight.css" | ||
} |
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,86 @@ | ||
import { | ||
history, | ||
indentWithTab | ||
} from "@codemirror/commands"; | ||
import { html } from "@codemirror/lang-html"; | ||
import { Compartment, EditorState } from '@codemirror/state'; | ||
import { EditorView, highlightActiveLineGutter, keymap, lineNumbers } from "@codemirror/view"; | ||
import { dracula, smoothy } from 'thememirror'; | ||
|
||
// Adapted from https://github.com/dotswan/filament-code-editor | ||
const CodeEditorAlpinePlugin = (Alpine) => { | ||
Alpine.data('codeEditorFormComponent', ({ state, isReadOnly, language = 'html' }) => ({ | ||
state, | ||
editor: undefined, | ||
themeConfig: undefined, | ||
languageConfig: undefined, | ||
isReadOnly: false, | ||
|
||
init() { | ||
this.isReadOnly = isReadOnly; | ||
this.themeConfig = new Compartment(); | ||
this.languageConfig = new Compartment(); | ||
this.render(); | ||
|
||
// Needed for programmatic updates from Livewire (e.g. form fill) to the component | ||
this.$watch('state', (value) => { | ||
if (this.editor.state.doc.toString() !== value) { | ||
this.editor.dispatch({ | ||
changes: { from: 0, to: this.editor.state.doc.length, insert: value } | ||
}); | ||
} | ||
}); | ||
}, | ||
|
||
render() { | ||
this.editor = new EditorView({ | ||
parent: this.$refs.codeEditor, | ||
state: EditorState.create({ | ||
doc: this.state, | ||
autofocus: true, | ||
indentWithTabs: true, | ||
smartIndent: true, | ||
lineNumbers: true, | ||
matchBrackets: true, | ||
tabSize: 2, | ||
styleSelectedText: true, | ||
extensions: [ | ||
keymap.of([indentWithTab]), | ||
this.languageConfig.of(language === 'json' ? json() : html()), | ||
this.themeConfig.of([dracula]), | ||
EditorView.lineWrapping, | ||
EditorState.readOnly.of(this.isReadOnly), | ||
lineNumbers(), | ||
history(), | ||
highlightActiveLineGutter(), | ||
EditorView.updateListener.of((v) => { | ||
if (v.docChanged) { | ||
this.state = v.state.doc.toString(); | ||
this.$wire.$commit(); | ||
} | ||
}), | ||
], | ||
}), | ||
}); | ||
|
||
window.addEventListener('theme-changed', () => { | ||
let theme = localStorage.getItem('theme'); | ||
if (theme === 'system') { | ||
theme = (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) | ||
? 'dark' | ||
: 'light'; | ||
} | ||
|
||
this.editor.dispatch({ | ||
effects: this.themeConfig.reconfigure([ | ||
theme === 'light' ? smoothy : dracula | ||
]) | ||
}); | ||
}); | ||
}, | ||
})); | ||
} | ||
|
||
document.addEventListener('alpine:init', () => { | ||
window.Alpine.plugin(CodeEditorAlpinePlugin); | ||
}); |
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 |
---|---|---|
@@ -1,39 +1,17 @@ | ||
{{-- Adapted from https://github.com/dotswan/filament-code-editor --}} | ||
<x-dynamic-component :component="$getFieldWrapperView()" :field="$field"> | ||
<div x-data="codeEditor" class="w-full max-h-full rounded-lg shadow-sm ring-1 transition duration-75 bg-white dark:bg-white/5 [&:not(:has(.fi-ac-action:focus))]:focus-within:ring-2 ring-gray-950/10 dark:ring-white/20 [&:not(:has(.fi-ac-action:focus))]:focus-within:ring-primary-600 dark:[&:not(:has(.fi-ac-action:focus))]:focus-within:ring-primary-500 overflow-hidden"> | ||
<div x-ref="editor" class="w-full h-[300px]" wire:ignore></div> | ||
<div class="relative max-w-full overflow-hidden rounded-lg shadow-sm ring-1 transition duration-75 bg-white dark:bg-white/5 [&:not(:has(.fi-ac-action:focus))]:focus-within:ring-2 ring-gray-950/10 dark:ring-white/20 [&:not(:has(.fi-ac-action:focus))]:focus-within:ring-primary-600 dark:[&:not(:has(.fi-ac-action:focus))]:focus-within:ring-primary-500 h-[365px]"> | ||
<div | ||
class="w-full h-full" | ||
x-data="codeEditorFormComponent({ | ||
state: $wire.$entangle('{{ $getStatePath() }}'), | ||
isReadOnly: @js($isDisabled()), | ||
})"> | ||
<div | ||
wire:ignore | ||
x-ref="codeEditor" | ||
class="w-full h-full"> | ||
</div> | ||
</div> | ||
</div> | ||
</x-dynamic-component> | ||
|
||
<script type="module"> | ||
import * as monaco from 'https://cdn.jsdelivr.net/npm/[email protected]/+esm'; | ||
Alpine.data('codeEditor', () => ({ | ||
state: @entangle($getStatePath()), | ||
init() { | ||
const editor = monaco.editor.create(this.$refs.editor, { | ||
value: this.state, | ||
language: 'twig', | ||
automaticLayout: true, | ||
scrollBeyondLastLine: false, | ||
readOnly: {{ $isDisabled() ? 'true' : 'false' }}, | ||
minimap: { | ||
enabled: false, | ||
}, | ||
}); | ||
editor.getModel().onDidChangeContent(() => this.state = editor.getValue()); | ||
window.addEventListener('theme-changed', () => { | ||
let theme = localStorage.getItem('theme'); | ||
if (theme === 'system') { | ||
theme = (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) | ||
? 'dark' | ||
: 'light'; | ||
} | ||
monaco.editor.setTheme(theme === 'light' ? 'vs' : 'vs-dark'); | ||
}); | ||
} | ||
})); | ||
</script> |
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,26 @@ | ||
<!DOCTYPE html> | ||
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
|
||
<meta name="application-name" content="{{ config('app.name') }}" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<meta name="csrf_token" value="{{ csrf_token() }}"/> | ||
|
||
<title>MailCarrier preview</title> | ||
|
||
<style> | ||
[x-cloak] { | ||
display: none !important; | ||
} | ||
</style> | ||
|
||
@livewireStyles | ||
</head> | ||
|
||
<body class="antialiased"> | ||
{{ $slot }} | ||
|
||
@livewireScripts | ||
</body> | ||
</html> |
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,43 @@ | ||
<div | ||
class="filament-peek-panel filament-peek-editor" | ||
x-bind:style="editorStyle" | ||
x-ref="builderEditor" | ||
@if ($this->canAutoRefresh()) data-auto-refresh-strategy="{{ $this->autoRefreshStrategy }}" @endif | ||
@if ($this->shouldAutoRefresh()) data-should-auto-refresh="1" @endif | ||
> | ||
<div class="filament-peek-panel-header"> | ||
<div x-text="editorTitle"></div> | ||
</div> | ||
|
||
<div | ||
class="filament-peek-panel-body" | ||
x-on:focusout="onEditorFocusOut($event)" | ||
> | ||
<div | ||
x-bind:class="{ | ||
'filament-peek-builder-editor': true, | ||
'has-sidebar-actions': editorHasSidebarActions, | ||
}" | ||
> | ||
<div class="filament-peek-builder-content"> | ||
<form wire:submit="submit"> | ||
{{ $this->form }} | ||
|
||
<button type="submit" style="display: none"> | ||
{{ __('filament-peek::ui.refresh-action-label') }} | ||
</button> | ||
</form> | ||
|
||
<x-filament-actions::modals /> | ||
</div> | ||
|
||
<div class="filament-peek-builder-actions"></div> | ||
|
||
<div | ||
class="filament-peek-editor-resizer" | ||
x-on:mousedown="onEditorResizerMouseDown($event)" | ||
x-bind:style="{display: editorIsResizable ? 'initial' : 'none'}" | ||
></div> | ||
</div> | ||
</div> | ||
</div> |
62 changes: 62 additions & 0 deletions
62
resources/views/livewire/preview/partials/modal-actions.blade.php
Large diffs are not rendered by default.
Oops, something went wrong.
102 changes: 102 additions & 0 deletions
102
resources/views/livewire/preview/preview-modal.blade.php
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,102 @@ | ||
@if (\Pboivin\FilamentPeek\Support\View::needsPreviewModal()) | ||
<div | ||
role="alertdialog" | ||
aria-modal="true" | ||
aria-labelledby="filament-peek-modal-title" | ||
x-data="PeekPreviewModal({ | ||
devicePresets: @js(config('filament-peek.devicePresets', false)), | ||
initialDevicePreset: @js(config('filament-peek.initialDevicePreset', 'fullscreen')), | ||
allowIframeOverflow: @js(config('filament-peek.allowIframeOverflow', false)), | ||
shouldCloseModalWithEscapeKey: @js(config('filament-peek.closeModalWithEscapeKey', true)), | ||
editorAutoRefreshDebounceTime: @js(config('filament-peek.builderEditor.autoRefreshDebounceMilliseconds', 500)), | ||
shouldRestoreIframePositionOnRefresh: @js(config('filament-peek.builderEditor.preservePreviewScrollPosition', false)), | ||
canResizeEditorSidebar: @js(config('filament-peek.builderEditor.canResizeSidebar', true)), | ||
editorSidebarMinWidth: @js(config('filament-peek.builderEditor.sidebarMinWidth', '30rem')), | ||
editorSidebarInitialWidth: @js(config('filament-peek.builderEditor.sidebarInitialWidth', '30rem')), | ||
})" | ||
x-bind:class="{ | ||
'filament-peek-modal': true, | ||
'is-filament-peek-editor-resizing': editorIsResizing, | ||
}" | ||
x-bind:style="modalStyle" | ||
x-on:open-preview-modal.window="onOpenPreviewModal($event)" | ||
x-on:refresh-preview-modal.window="onRefreshPreviewModal($event)" | ||
x-on:close-preview-modal.window="onClosePreviewModal($event)" | ||
x-on:keyup.escape.window="handleEscapeKey()" | ||
x-on:mouseup.window="onMouseUp($event)" | ||
x-on:mousemove.debounce.5ms.window="onMouseMove($event)" | ||
x-trap="isOpen" | ||
x-cloak | ||
> | ||
@if (\Pboivin\FilamentPeek\Support\View::needsBuilderEditor()) | ||
@livewire('filament-peek::builder-editor') | ||
@endif | ||
|
||
<div class="filament-peek-panel filament-peek-preview"> | ||
<div class="filament-peek-panel-header"> | ||
<div | ||
id="filament-peek-modal-title" | ||
class="filament-peek-modal-title" | ||
x-text="modalTitle" | ||
></div> | ||
|
||
@if (config('filament-peek.devicePresets', false)) | ||
<div class="filament-peek-device-presets"> | ||
@foreach (config('filament-peek.devicePresets') as $presetName => $presetConfig) | ||
<button | ||
type="button" | ||
data-preset-name="{{ $presetName }}" | ||
x-on:click="setDevicePreset('{{ $presetName }}')" | ||
x-bind:class="{'is-active-device-preset': isActiveDevicePreset('{{ $presetName }}')}" | ||
> | ||
<x-filament::icon | ||
:icon="$presetConfig['icon'] ?? 'heroicon-o-computer-desktop'" | ||
:class="Arr::toCssClasses(['rotate-90' => $presetConfig['rotateIcon'] ?? false])" | ||
/> | ||
</button> | ||
@endforeach | ||
|
||
<button | ||
type="button" | ||
class="filament-peek-rotate-preset" | ||
x-on:click="rotateDevicePreset()" | ||
x-bind:disabled="!canRotatePreset" | ||
> | ||
@include('filament-peek::partials.icon-rotate') | ||
</button> | ||
</div> | ||
@endif | ||
|
||
<div class="filament-peek-modal-actions"> | ||
@include('mailcarrier::livewire.preview.partials.modal-actions') | ||
</div> | ||
</div> | ||
|
||
<div | ||
x-ref="previewModalBody" | ||
class="{{ Arr::toCssClasses([ | ||
'filament-peek-panel-body' => true, | ||
'allow-iframe-overflow' => config('filament-peek.allowIframeOverflow', false), | ||
]) }}" | ||
> | ||
<template x-if="iframeUrl"> | ||
<iframe | ||
x-bind:src="iframeUrl" | ||
x-bind:style="iframeStyle" | ||
frameborder="0" | ||
></iframe> | ||
</template> | ||
|
||
<template x-if="!iframeUrl && iframeContent"> | ||
<iframe | ||
x-bind:srcdoc="iframeContent" | ||
x-bind:style="iframeStyle" | ||
frameborder="0" | ||
></iframe> | ||
</template> | ||
|
||
<div class="filament-peek-iframe-cover"></div> | ||
</div> | ||
</div> | ||
</div> | ||
@endif |
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.