| 1 |
import { |
| 2 |
applyObjectPropertyDifference, |
| 3 |
getEmbeddedProps, |
| 4 |
StateChangeManager, |
| 5 |
useEmbeddedState, |
| 6 |
} from "@html_editor/others/embedded_component_utils"; |
| 7 |
import { useEffect, useRef, useState } from "@odoo/owl"; |
| 8 |
import { ReadonlyEmbeddedFileComponent } from "@html_editor/others/embedded_components/core/file/readonly_file"; |
| 9 |
|
| 10 |
export class EmbeddedFileComponent extends ReadonlyEmbeddedFileComponent { |
| 11 |
static template = "html_editor.EmbeddedFile"; |
| 12 |
|
| 13 |
setup() { |
| 14 |
super.setup(); |
| 15 |
// override the state by an embedded state. |
| 16 |
this.state = useEmbeddedState(this.props.host); |
| 17 |
this.fileModel.state = this.state; |
| 18 |
this.localState = useState({ |
| 19 |
editFileName: false, |
| 20 |
}); |
| 21 |
this.nameInput = useRef("nameInput"); |
| 22 |
useEffect( |
| 23 |
() => { |
| 24 |
if (this.localState.editFileName) { |
| 25 |
this.nameInput.el.focus(); |
| 26 |
this.nameInput.el.select(); |
| 27 |
} |
| 28 |
}, |
| 29 |
() => [this.localState.editFileName] |
| 30 |
); |
| 31 |
} |
| 32 |
|
| 33 |
onBlurNameInput(ev) { |
| 34 |
this.localState.editFileName = false; |
| 35 |
this.renameFile(); |
| 36 |
} |
| 37 |
|
| 38 |
onFocusFileName(ev) { |
| 39 |
this.localState.editFileName = true; |
| 40 |
} |
| 41 |
|
| 42 |
onKeydownNameInput(ev) { |
| 43 |
if (ev.key !== "Enter") { |
| 44 |
return; |
| 45 |
} else { |
| 46 |
ev.preventDefault(); |
| 47 |
} |
| 48 |
if (this.renameFile()) { |
| 49 |
this.localState.editFileName = false; |
| 50 |
this.env.editorShared?.setSelectionAfter(this.props.host); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
renameFile() { |
| 55 |
let newName = this.nameInput.el.value; |
| 56 |
if (!newName.length) { |
| 57 |
return false; |
| 58 |
} |
| 59 |
if (newName === this.fileModel.filename) { |
| 60 |
return true; |
| 61 |
} |
| 62 |
// filename is the name of the file as written in the editor by the |
| 63 |
// user. It does not necessarily have the file extension. |
| 64 |
this.fileModel.filename = newName; |
| 65 |
if (this.fileModel.extension) { |
| 66 |
const pattern = new RegExp(`\\.${this.fileModel.extension}$`, "i"); |
| 67 |
if (!newName.match(pattern)) { |
| 68 |
newName += `.${this.fileModel.extension}`; |
| 69 |
} |
| 70 |
} |
| 71 |
// name is the full name of the file (always with extension) |
| 72 |
// and is used as the url queryParam when downloading it. |
| 73 |
this.fileModel.name = newName; |
| 74 |
return true; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
export const fileEmbedding = { |
| 79 |
name: "file", |
| 80 |
Component: EmbeddedFileComponent, |
| 81 |
getProps: (host) => ({ host, ...getEmbeddedProps(host) }), |
| 82 |
getStateChangeManager: (config) => |
| 83 |
new StateChangeManager( |
| 84 |
Object.assign(config, { |
| 85 |
propertyUpdater: { |
| 86 |
fileData: (state, previous, next) => { |
| 87 |
applyObjectPropertyDifference( |
| 88 |
state, |
| 89 |
"fileData", |
| 90 |
previous.fileData, |
| 91 |
next.fileData |
| 92 |
); |
| 93 |
}, |
| 94 |
}, |
| 95 |
}) |
| 96 |
), |
| 97 |
}; |