-
-
Notifications
You must be signed in to change notification settings - Fork 644
[6.x] Add Cloudflare Stream support to the video fieldtype #15459
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
edalzell
wants to merge
16
commits into
statamic:6.x
Choose a base branch
from
edalzell:feature/cloudflare-stream-video
base: 6.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
aed6a73
Augment video fields to a value object carrying the provider
edalzell c44253e
Accept null in the video embed url helpers
edalzell 862f032
Base video embed truthiness on the value, not the provider
edalzell f17b781
Pass unsupported video urls through the embed url modifier
edalzell 3745c70
Keep trackable embed urls trackable for augmented videos
edalzell 9240993
Treat video files as supported but not embeddable
edalzell 83df089
Detect oembed providers before falling back to video files
edalzell dc360f7
Keep video fields serializing to a string in the api
edalzell 56acf47
Assert augmented video fields behave like the raw value
edalzell cdbcc04
Update video embed tests for the corrected contract
edalzell 81c399a
Measure the string length of stringable value objects
edalzell b752d12
Add Cloudflare Stream support to the video fieldtype
edalzell 2f08f55
Update resources/js/components/fieldtypes/VideoFieldtype.vue
edalzell 57a273d
Update resources/js/components/fieldtypes/VideoFieldtype.vue
edalzell effa722
Update resources/js/components/fieldtypes/VideoFieldtype.vue
edalzell 6468274
Show a dedicated message for an invalid Cloudflare ID
edalzell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,6 +1,27 @@ | ||
| <template> | ||
| <div class="flex flex-col space-y-3 p-1.5 bg-gray-100 border border-gray-300 dark:bg-gray-900 dark:border-gray-700 rounded-xl"> | ||
| <ui-input-group> | ||
| <ui-combobox | ||
| :model-value="isCloudflare ? 'cloudflare' : 'url'" | ||
| :options="meta.providers" | ||
| option-label="label" | ||
| option-value="value" | ||
| :read-only="isReadOnly" | ||
| :aria-label="__('Video Provider')" | ||
| @update:model-value="changeMode" | ||
| /> | ||
|
edalzell marked this conversation as resolved.
|
||
| <ui-input-group v-if="isCloudflare"> | ||
| <ui-input-group-prepend :text="__('ID')" /> | ||
| <ui-input | ||
| :model-value="videoId" | ||
| :isReadOnly="isReadOnly" | ||
| :aria-label="__('Video ID')" | ||
| @update:model-value="updateCloudflareId" | ||
| @focus="$emit('focus')" | ||
| @blur="$emit('blur')" | ||
| input-class="border-s-0" | ||
| /> | ||
| </ui-input-group> | ||
| <ui-input-group v-else> | ||
| <ui-input-group-prepend :text="__('URL')" /> | ||
| <ui-input | ||
| :model-value="value" | ||
|
|
@@ -13,7 +34,7 @@ | |
| input-class="border-s-0" | ||
| /> | ||
| </ui-input-group> | ||
| <ui-description v-if="isInvalid" class="text-red-600">{{ __('statamic::validation.url') }}</ui-description> | ||
| <ui-description v-if="isInvalid" class="text-red-600">{{ invalidMessage }}</ui-description> | ||
| <iframe | ||
| v-if="shouldShowPreview" | ||
| ref="iframe" | ||
|
|
@@ -29,22 +50,32 @@ | |
| <script> | ||
| import Fieldtype from './Fieldtype.vue'; | ||
|
|
||
| const CLOUDFLARE = 'cloudflare'; | ||
| const CLOUDFLARE_PREFIX = 'cloudflare:'; | ||
| const URL_MODE = 'url'; | ||
|
|
||
| export default { | ||
| mixins: [Fieldtype], | ||
|
|
||
| data() { | ||
| return { | ||
| isVisible: false, | ||
| observer: null, | ||
| // Only consulted when there's no value; otherwise the value itself says which input to show. | ||
| mode: this.meta.video?.provider === CLOUDFLARE ? CLOUDFLARE : URL_MODE, | ||
| }; | ||
| }, | ||
|
|
||
| computed: { | ||
| shouldShowPreview() { | ||
| return !this.isInvalid && (this.isEmbeddable || this.isVideo); | ||
| return !this.isInvalid && (this.isCloudflare ? !!this.videoId : this.isEmbeddable || this.isVideo); | ||
| }, | ||
|
|
||
| embedUrl() { | ||
| if (this.isCloudflare) { | ||
| return this.videoId ? `https://iframe.cloudflarestream.com/${this.videoId}` : null; | ||
| } | ||
|
|
||
| let embed_url = this.value || ''; | ||
|
|
||
| if (embed_url.includes('youtube')) { | ||
|
|
@@ -73,6 +104,16 @@ export default { | |
| return embed_url; | ||
| }, | ||
|
|
||
| invalidMessage() { | ||
| return this.isCloudflare | ||
| ? __('statamic::validation.video_fieldtype_cloudflare_id') | ||
| : __('statamic::validation.url'); | ||
| }, | ||
|
|
||
| isCloudflare() { | ||
| return this.value?.startsWith(CLOUDFLARE_PREFIX) || (!this.value && this.mode === CLOUDFLARE); | ||
| }, | ||
|
|
||
| isEmbeddable() { | ||
| const url = this.value || ''; | ||
| const isYoutube = url.includes('youtube') || url.includes('youtu.be'); | ||
|
|
@@ -81,6 +122,8 @@ export default { | |
| }, | ||
|
|
||
| isInvalid() { | ||
| if (this.isCloudflare) return !!this.videoId && !/^[a-zA-Z0-9]+$/.test(this.videoId); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor, but when this branch fails the message rendered below is
edalzell marked this conversation as resolved.
|
||
|
|
||
| let htmlRegex = new RegExp(/<([A-Z][A-Z0-9]*)\b[^>]*>.*?<\/\1>|<([A-Z][A-Z0-9]*)\b[^\/]*\/>/i); | ||
| return htmlRegex.test(this.value || ''); | ||
| }, | ||
|
|
@@ -95,6 +138,25 @@ export default { | |
| const isVideo = url.includes('.mp4') || url.includes('.ogv') || url.includes('.mov') || url.includes('.webm'); | ||
| return !this.isEmbeddable && isVideo; | ||
| }, | ||
|
|
||
| videoId() { | ||
| return this.value?.startsWith(CLOUDFLARE_PREFIX) ? this.value.slice(CLOUDFLARE_PREFIX.length) : null; | ||
| }, | ||
| }, | ||
|
|
||
| methods: { | ||
| changeMode(mode) { | ||
| if (mode === this.mode) return; | ||
|
|
||
| this.updateDebounced.cancel(); | ||
| this.mode = mode; | ||
|
|
||
| if (this.value) this.update(null); | ||
| }, | ||
|
edalzell marked this conversation as resolved.
|
||
|
|
||
| updateCloudflareId(id) { | ||
| this.update(id ? `${CLOUDFLARE_PREFIX}${id}` : null); | ||
| }, | ||
| }, | ||
|
|
||
| mounted() { | ||
|
|
||
This file contains hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The combobox needs
isReadOnlypassed through. Bothui-inputs get it, this doesn't, andui-comboboxsupports areadOnlyprop (resources/js/components/ui/Combobox/Combobox.vue:61) — so nothing stops the user changing the provider on a read-only field, andchangeMode()then callsthis.update(null).I confirmed it by mounting this component with
config: { visibility: 'read_only' }and a populated YouTube value:isReadOnlyistrue, and selecting Cloudflare emitsupdate:value→[null]. The field is now dirty and saving persists the wipe. Same applies tovisibility: computedfields.