| 1 |
import { |
| 2 |
getEmbeddedProps, |
| 3 |
StateChangeManager, |
| 4 |
useEmbeddedState, |
| 5 |
} from "@html_editor/others/embedded_component_utils"; |
| 6 |
import { getVideoUrl } from "@html_editor/utils/url"; |
| 7 |
import { |
| 8 |
Component, |
| 9 |
onMounted, |
| 10 |
onWillDestroy, |
| 11 |
onWillUnmount, |
| 12 |
useExternalListener, |
| 13 |
useRef, |
| 14 |
} from "@odoo/owl"; |
| 15 |
import { Dropdown } from "@web/core/dropdown/dropdown"; |
| 16 |
import { useDropdownState } from "@web/core/dropdown/dropdown_hooks"; |
| 17 |
import { DropdownItem } from "@web/core/dropdown/dropdown_item"; |
| 18 |
import { ReadonlyEmbeddedVideoComponent } from "../../core/video/readonly_video"; |
| 19 |
|
| 20 |
export class EmbeddedVideoComponent extends ReadonlyEmbeddedVideoComponent { |
| 21 |
static template = "html_editor.EmbeddedVideo"; |
| 22 |
static props = { |
| 23 |
platform: { type: String }, |
| 24 |
videoId: { type: String }, |
| 25 |
params: { type: Object, optional: true }, |
| 26 |
host: { type: HTMLElement }, |
| 27 |
createOverlay: { type: Function, optional: true }, |
| 28 |
focusEditable: { type: Function, optional: true }, |
| 29 |
addStep: { type: Function, optional: true }, |
| 30 |
openVideoSelectorDialog: { type: Function, optional: true }, |
| 31 |
}; |
| 32 |
|
| 33 |
setup() { |
| 34 |
super.setup(); |
| 35 |
this.videoBlock = this.props.host; |
| 36 |
this.state = useEmbeddedState(this.videoBlock); |
| 37 |
this.dropdown = useDropdownState(); |
| 38 |
|
| 39 |
this.videoSettingsOverlay = this.props.createOverlay(VideoSettings, { |
| 40 |
positionOptions: { |
| 41 |
position: "right-start", |
| 42 |
}, |
| 43 |
className: "video-overlay", |
| 44 |
closeOnPointerdown: false, |
| 45 |
}); |
| 46 |
this.iframeRef = useRef("iframeRef"); |
| 47 |
|
| 48 |
useExternalListener(this.videoBlock, "pointerenter", () => { |
| 49 |
this.videoSettingsOverlay.open({ |
| 50 |
target: this.videoBlock, |
| 51 |
props: { |
| 52 |
videoBlock: this.videoBlock, |
| 53 |
overlay: this.videoSettingsOverlay, |
| 54 |
replaceVideo: () => { |
| 55 |
this.props.openVideoSelectorDialog((media) => { |
| 56 |
this.replaceVideo(media); |
| 57 |
}, this.iframeRef.el); |
| 58 |
}, |
| 59 |
removeVideo: () => { |
| 60 |
this.videoBlock.remove(); |
| 61 |
this.props.addStep(); |
| 62 |
}, |
| 63 |
focusEditable: this.props.focusEditable, |
| 64 |
dropdown: this.dropdown, |
| 65 |
}, |
| 66 |
}); |
| 67 |
}); |
| 68 |
|
| 69 |
useExternalListener(this.videoBlock, "pointerleave", (e) => { |
| 70 |
if (this.dropdown.isOpen || e.relatedTarget?.closest(".video-overlay")) { |
| 71 |
return; |
| 72 |
} |
| 73 |
this.videoSettingsOverlay.close(); |
| 74 |
}); |
| 75 |
|
| 76 |
onWillDestroy(() => { |
| 77 |
this.videoSettingsOverlay?.close(); |
| 78 |
}); |
| 79 |
} |
| 80 |
|
| 81 |
get url() { |
| 82 |
return getVideoUrl(this.state.platform, this.state.videoId, this.state.params).toString(); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Replace a video in the editor |
| 87 |
* @param {Object} media |
| 88 |
*/ |
| 89 |
replaceVideo(media) { |
| 90 |
this.state.videoId = media.videoId; |
| 91 |
this.state.platform = media.platform; |
| 92 |
this.state.params = media.params; |
| 93 |
this.props.focusEditable(); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
export const videoEmbedding = { |
| 98 |
name: "video", |
| 99 |
Component: EmbeddedVideoComponent, |
| 100 |
getProps: (host) => ({ host, ...getEmbeddedProps(host) }), |
| 101 |
getStateChangeManager: (config) => new StateChangeManager(config), |
| 102 |
}; |
| 103 |
|
| 104 |
export class VideoSettings extends Component { |
| 105 |
static template = "html_editor.VideoSettings"; |
| 106 |
static components = { Dropdown, DropdownItem }; |
| 107 |
static props = { |
| 108 |
videoBlock: { type: HTMLElement }, |
| 109 |
overlay: { type: Object }, |
| 110 |
replaceVideo: { type: Function }, |
| 111 |
removeVideo: { type: Function }, |
| 112 |
focusEditable: { type: Function }, |
| 113 |
dropdown: { type: Object }, |
| 114 |
}; |
| 115 |
|
| 116 |
setup() { |
| 117 |
this.menuRef = useRef("menuRef"); |
| 118 |
|
| 119 |
onMounted(() => { |
| 120 |
this.menuRef.el.addEventListener("pointerleave", () => { |
| 121 |
if (!this.props.dropdown.isOpen) { |
| 122 |
this.props.overlay.close(); |
| 123 |
} |
| 124 |
}); |
| 125 |
}); |
| 126 |
|
| 127 |
useExternalListener(document, "pointerdown", (ev) => { |
| 128 |
if (this.props.dropdown.isOpen) { |
| 129 |
return; |
| 130 |
} |
| 131 |
this.props.overlay.close(); |
| 132 |
}); |
| 133 |
|
| 134 |
onWillUnmount(() => { |
| 135 |
if (!this.props.videoBlock.isConnected) { |
| 136 |
this.props.focusEditable(); |
| 137 |
} |
| 138 |
}); |
| 139 |
} |
| 140 |
} |