Skip to content

Commit

Permalink
Merge pull request #95 from mostafaznv/dev
Browse files Browse the repository at this point in the history
feat: upgrade nova-video to v6
  • Loading branch information
mostafaznv authored Nov 1, 2023
2 parents 42fa7f9 + 56f586c commit 8226818
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 26 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"require": {
"php": "^8.0.2",
"intervention/image": "^2.5",
"mostafaznv/nova-video": "^3.0|^4.0|^5.0",
"mostafaznv/nova-video": "^3.0|^4.0|^5.0|^6.0.2",
"laravel/framework": "^8.40.0|^9.0|^10.0",
"laravel/nova": "^4.13",
"nova-kit/nova-packages-tool": "^1.2",
Expand Down
2 changes: 1 addition & 1 deletion dist/js/field.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/getting-started/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ composer require mostafaznv/nova-ckeditor
php artisan vendor:publish --provider="Mostafaznv\NovaCkEditor\FieldServiceProvider"
```

#### 3. Prepare the migration and models
#### 3. Prepare the migration, configurations and models

After publishing stubs, essential `Image`, `Video` and `Audio` classes will be created in the `app/Models` and `app/Nova/Resources` directories respectively. These classes are used for the `media-picker` in the CKEditor field.

Expand Down
26 changes: 23 additions & 3 deletions resources/js/components/editor-field.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,29 @@

<p v-if="currentField.helpText" v-html="currentField.helpText" class="help-text help-text mt-2" />

<media-browser @select="$options[editorName].execute('imageBrowser', $event)" type="image" :field-key="$options[editorUUID] + '-image'" :multiple="true" />
<media-browser @select="$options[editorName].execute('videoBrowser', $event)" type="video" :field-key="$options[editorUUID] + '-video'" :multiple="true" :has-larupload-trait="currentField.videoHasLaruploadTrait" />
<media-browser @select="$options[editorName].execute('audioBrowser', $event)" type="audio" :field-key="$options[editorUUID] + '-audio'" :multiple="true" />
<media-browser
@select="$options[editorName].execute('imageBrowser', $event)"
type="image"
:field-key="$options[editorUUID] + '-image'"
:multiple="true"
/>

<media-browser
@select="$options[editorName].execute('videoBrowser', $event)"
type="video"
:field-key="$options[editorUUID] + '-video'"
:multiple="true"
:has-larupload-trait="currentField.videoHasLaruploadTrait"
:nova-video-is-legacy="currentField.novaVideoIsLegacy"
/>

<media-browser
@select="$options[editorName].execute('audioBrowser', $event)"
type="audio"
:field-key="$options[editorUUID] + '-audio'"
:multiple="true"
/>

<snippet-browser :field-key="$options[editorUUID]" :snippets="currentField.snippetBrowser" />
</template>
</default-field>
Expand Down
28 changes: 23 additions & 5 deletions resources/js/components/media-browser.vue
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ export default {
type: Boolean,
default: false
},
novaVideoIsLegacy: {
type: Boolean,
default: true
},
type: {
type: String,
default: 'image',
Expand Down Expand Up @@ -310,10 +314,19 @@ export default {
if (this.isVideoPicker) {
this.items.map((item) => {
if (this.hasLaruploadTrait) {
item.url = item.file_field.cover
item.urls = {
cover: item.file_field.cover,
video: item.file_field.original,
if (this.novaVideoIsLegacy) {
item.url = item.file_field.cover
item.urls = {
cover: item.file_field.cover,
video: item.file_field.original,
}
}
else {
item.url = item.file.cover
item.urls = {
cover: item.file.cover,
video: item.file.original,
}
}
}
else {
Expand Down Expand Up @@ -378,7 +391,12 @@ export default {
}
else {
if (this.hasLaruploadTrait) {
data.file_field = file
if (this.novaVideoIsLegacy) {
data.file_field = file
}
else {
data['file[original]'] = file
}
}
else {
data.file = file
Expand Down
9 changes: 5 additions & 4 deletions resources/js/components/mixins/interactsWithResources.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ export default {
*/
resourceToObject({fields}) {
return fields.reduce((obj, item) => {
if (item.value) {
obj[item.attribute] = item.value
}
else if (item.displayedAs) {
if (item.displayedAs) {
obj[item.attribute] = item.displayedAs
}
else if (item.value) {
obj[item.attribute] = item.value
}
else {
obj[item.attribute] = null
}
Expand Down Expand Up @@ -70,6 +70,7 @@ export default {
async uploadResource(resourceKey, params) {
const data = new FormData
Object.entries(params).forEach(([key, value]) => data.append(key, value))

return await Nova.request()
.post(`/nova-api/${resourceKey}`, data)
.then(({data}) => data.resource)
Expand Down
34 changes: 25 additions & 9 deletions src/CkEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Laravel\Nova\Fields\Field;
use Laravel\Nova\Fields\SupportsDependentFields;
use Mostafaznv\Larupload\Traits\Larupload;
use Mostafaznv\NovaVideo\Video;

/**
* @method static static make(mixed $name, string|\Closure|callable|object|null $attribute = null, callable|null $resolveCallback = null)
Expand Down Expand Up @@ -299,15 +300,32 @@ public function jsonSerialize(): array
'shouldNotGroupWhenFull' => $this->shouldNotGroupWhenFull,
'shouldShow' => $this->shouldBeExpanded(),
'videoHasLaruploadTrait' => $this->hasLaruploadTrait(),
'novaVideoIsLegacy' => $this->isLegacyNovaVideo(),
]);
}

/**
* Render Snippets as HTML
*
* @param array $snippets
* @return array
*/
protected function prepareSnippets(array $snippets): array
{
foreach ($snippets as $key => $snippet) {
$snippet['html'] = view($snippet['html'])->render();
$snippets[$key] = $snippet;
}

return $snippets;
}

/**
* Check if class has larupload trait
*
* @return bool
*/
protected function hasLaruploadTrait(): bool
private function hasLaruploadTrait(): bool
{
if ($this->videoModel) {
$class = $this->videoModel;
Expand All @@ -319,19 +337,17 @@ protected function hasLaruploadTrait(): bool
}

/**
* Render Snippets as HTML
* Check whether NovaVideo is legacy
*
* @param array $snippets
* @return array
* @return bool
*/
protected function prepareSnippets(array $snippets): array
private function isLegacyNovaVideo(): bool
{
foreach ($snippets as $key => $snippet) {
$snippet['html'] = view($snippet['html'])->render();
$snippets[$key] = $snippet;
if (property_exists(Video::class, 'storeWithLarupload')) {
return false;
}

return $snippets;
return true;
}

private function prepareToolbar(string $toolbar, array $items = null): void
Expand Down
14 changes: 12 additions & 2 deletions src/VideoUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,19 @@ class VideoUpload extends Video
public function __construct(string $label, string $fieldName, string $disk = 'video')
{
if ($this->hasLaruploadTrait()) {
parent::__construct($label, $fieldName . '_field', $disk);
# version 6.* and higher
if (property_exists($this, 'storeWithLarupload')) {
parent::__construct($label, $fieldName, $disk);

$this->storeWithLarupload();
}
# older versions
else {
parent::__construct($label, $fieldName . '_field', $disk);

$this->storeWithLarupload($fieldName);
}

$this->storeWithLarupload($fieldName);
}
else {
parent::__construct($label, $fieldName, $disk);
Expand Down

0 comments on commit 8226818

Please sign in to comment.