From 283bdacb934f088a950562c6a31b373879d8e859 Mon Sep 17 00:00:00 2001 From: Dmitry Kuzin Date: Tue, 3 Oct 2023 11:56:47 +0400 Subject: [PATCH 1/2] Allow to customize file preview component --- .../src/angular-ui.module.ts | 5 +- .../file-actions/file-preview.component.html | 30 ++++++++ .../file-actions/file-preview.component.ts | 15 ++++ .../src/questions/file.component.html | 30 +------- .../src/questions/file.component.ts | 6 +- .../FileChooseButton.vue | 0 .../src/{ => components/file}/FilePreview.vue | 0 packages/survey-vue3-ui/src/index.ts | 4 +- src/entries/knockout-ui-model.ts | 3 +- src/entries/react-ui-model.ts | 3 +- src/entries/vue-ui-model.ts | 4 +- .../{file-actions => file}/choose-file.html | 0 .../{file-actions => file}/choose-file.ts | 0 .../components/file/file-preview.html | 25 ++++++ src/knockout/components/file/file-preview.ts | 14 ++++ src/knockout/templates/question-file.html | 40 ++-------- .../file-choose-button.tsx | 0 src/react/components/file/file-preview.tsx | 76 +++++++++++++++++++ src/react/reactquestion_file.tsx | 59 +------------- .../file-choose-button.vue | 0 .../{ => components/file}/file-preview.vue | 0 21 files changed, 183 insertions(+), 131 deletions(-) create mode 100644 packages/survey-angular-ui/src/components/file-actions/file-preview.component.html create mode 100644 packages/survey-angular-ui/src/components/file-actions/file-preview.component.ts rename packages/survey-vue3-ui/src/components/{file-actions => file}/FileChooseButton.vue (100%) rename packages/survey-vue3-ui/src/{ => components/file}/FilePreview.vue (100%) rename src/knockout/components/{file-actions => file}/choose-file.html (100%) rename src/knockout/components/{file-actions => file}/choose-file.ts (100%) create mode 100644 src/knockout/components/file/file-preview.html create mode 100644 src/knockout/components/file/file-preview.ts rename src/react/components/{file-actions => file}/file-choose-button.tsx (100%) create mode 100644 src/react/components/file/file-preview.tsx rename src/vue/components/{file-actions => file}/file-choose-button.vue (100%) rename src/vue/{ => components/file}/file-preview.vue (100%) diff --git a/packages/survey-angular-ui/src/angular-ui.module.ts b/packages/survey-angular-ui/src/angular-ui.module.ts index cb060f1459..cdd8a0a8cd 100644 --- a/packages/survey-angular-ui/src/angular-ui.module.ts +++ b/packages/survey-angular-ui/src/angular-ui.module.ts @@ -115,6 +115,7 @@ import { LoadingIndicatorComponent } from "./angular-ui"; import { CoverComponent } from "./components/cover/cover.component"; import { CoverCellComponent } from "./components/cover/cover-cell.component"; import { ChooseFileBtn } from "./components/file-actions/choose-file.component"; +import { FilePreviewComponent } from "./components/file-actions/file-preview.component"; @NgModule({ declarations: [ @@ -132,7 +133,7 @@ import { ChooseFileBtn } from "./components/file-actions/choose-file.component"; MultipleTextComponent, MultipleTextItemComponent, DynamicComponentDirective, RankingQuestionComponent, RankingItemComponent, PanelDynamicQuestionComponent, EmbeddedViewContentComponent, CustomWidgetComponent, MatrixCellComponent, MatrixTableComponent, MatrixDropdownComponent, MatrixDynamicComponent, MatrixDetailButtonComponent, MatrixDynamicRemoveButtonComponent, MatrixDynamicDragDropIconComponent, MatrixRequiredHeader, ExpressionComponent, SafeResourceUrlPipe, BrandInfoComponent, CustomQuestionComponent, CompositeQuestionComponent, ButtonGroupItemComponent, ButtonGroupQuestionComponent, MatrixRowComponent, ModalComponent, LogoImageComponent, SkeletonComponent, TimerPanelComponent, PaneldynamicRemoveButtonComponent, - NotifierComponent, ComponentsContainerComponent, MultipleTextRowComponent, LoadingIndicatorComponent, CoverComponent, CoverCellComponent, ChooseFileBtn + NotifierComponent, ComponentsContainerComponent, MultipleTextRowComponent, LoadingIndicatorComponent, CoverComponent, CoverCellComponent, ChooseFileBtn, FilePreviewComponent ], imports: [ CommonModule, FormsModule @@ -153,7 +154,7 @@ import { ChooseFileBtn } from "./components/file-actions/choose-file.component"; MultipleTextComponent, MultipleTextItemComponent, DynamicComponentDirective, RankingQuestionComponent, RankingItemComponent, PanelDynamicQuestionComponent, EmbeddedViewContentComponent, CustomWidgetComponent, MatrixCellComponent, MatrixTableComponent, MatrixDropdownComponent, MatrixDynamicComponent, MatrixDetailButtonComponent, MatrixDynamicRemoveButtonComponent, MatrixDynamicDragDropIconComponent, MatrixRequiredHeader, ExpressionComponent, SafeResourceUrlPipe, CustomQuestionComponent, CompositeQuestionComponent, ButtonGroupQuestionComponent, ModalComponent, LogoImageComponent, SkeletonComponent, TimerPanelComponent, PaneldynamicRemoveButtonComponent, - NotifierComponent, ComponentsContainerComponent, MultipleTextRowComponent, LoadingIndicatorComponent, CoverComponent, CoverCellComponent + NotifierComponent, ComponentsContainerComponent, MultipleTextRowComponent, LoadingIndicatorComponent, CoverComponent, CoverCellComponent, FilePreviewComponent ], providers: [PopupService], }) diff --git a/packages/survey-angular-ui/src/components/file-actions/file-preview.component.html b/packages/survey-angular-ui/src/components/file-actions/file-preview.component.html new file mode 100644 index 0000000000..e4c4e15017 --- /dev/null +++ b/packages/survey-angular-ui/src/components/file-actions/file-preview.component.html @@ -0,0 +1,30 @@ + +
+ +
+ {{ val.name + }} +
+
+ File preview + +
+ {{ question.removeFileCaption }} + +
+
+
+ {{ val.name + }} +
+
+
+
\ No newline at end of file diff --git a/packages/survey-angular-ui/src/components/file-actions/file-preview.component.ts b/packages/survey-angular-ui/src/components/file-actions/file-preview.component.ts new file mode 100644 index 0000000000..b821433fe9 --- /dev/null +++ b/packages/survey-angular-ui/src/components/file-actions/file-preview.component.ts @@ -0,0 +1,15 @@ +import { Action, QuestionFileModel } from "survey-core"; +import { AngularComponentFactory } from "../../component-factory"; +import { Component, Input } from "@angular/core"; +import { EmbeddedViewContentComponent } from "../../embedded-view-content.component"; +@Component({ + selector: "sv-ng-file-preview", + templateUrl: "./file-preview.component.html" +}) +export class FilePreviewComponent extends EmbeddedViewContentComponent { + @Input() question!: QuestionFileModel; + trackFilesFn: (index: number) => string = (index: number): string => { + return this.question.inputId + "_" + index; + } +} +AngularComponentFactory.Instance.registerComponent("sv-file-preview", FilePreviewComponent); \ No newline at end of file diff --git a/packages/survey-angular-ui/src/questions/file.component.html b/packages/survey-angular-ui/src/questions/file.component.html index b129b57286..422b846206 100644 --- a/packages/survey-angular-ui/src/questions/file.component.html +++ b/packages/survey-angular-ui/src/questions/file.component.html @@ -28,7 +28,7 @@ - + @@ -36,34 +36,6 @@ - -
- - -
- File preview - -
- {{ model.removeFileCaption }} - -
-
- -
-
-
- - \ No newline at end of file diff --git a/src/react/components/file-actions/file-choose-button.tsx b/src/react/components/file/file-choose-button.tsx similarity index 100% rename from src/react/components/file-actions/file-choose-button.tsx rename to src/react/components/file/file-choose-button.tsx diff --git a/src/react/components/file/file-preview.tsx b/src/react/components/file/file-preview.tsx new file mode 100644 index 0000000000..a80281961d --- /dev/null +++ b/src/react/components/file/file-preview.tsx @@ -0,0 +1,76 @@ +import React from "react"; +import { SurveyElementBase } from "../../reactquestion_element"; +import { QuestionFileModel } from "survey-core"; +import { SvgIcon } from "../svg-icon/svg-icon"; +import { ReactElementFactory } from "../../element-factory"; + +export class SurveyFilePreview extends SurveyElementBase<{ question: QuestionFileModel }, {}> { + + protected get question(): QuestionFileModel { + return this.props.question; + } + + protected renderFileSign(className: string, val: any): JSX.Element | null { + if(!className || !val.name) return null; + return ( +
+ { + this.question.doDownloadFile(event, val); + }} + title={val.name} + download={val.name} + style={{ width: this.question.imageWidth }} + > + {val.name} + +
+ ); + } + + protected renderElement(): JSX.Element | null { + var previews = this.question.previewValue.map((val, index) => { + if (!val) return null; + return ( + + {this.renderFileSign(this.question.cssClasses.fileSign, val)} +
+ {this.question.canPreviewImage(val) ? ( + File preview + ) : (this.question.cssClasses.defaultImage?( + + ):null)} + {val.name && !this.question.isReadOnly ? ( +
this.question.doRemoveFile(val)}> + + {this.question.removeFileCaption} + + {(this.question.cssClasses.removeFileSvgIconId) ? + (): null } +
+ ) : null} +
+ {this.renderFileSign(this.question.cssClasses.fileSignBottom, val)} +
+ ); + }); + return
{previews}
; + } +} +ReactElementFactory.Instance.registerElement( + "sv-file-preview", + (props) => { + return React.createElement(SurveyFilePreview, props); + } +); \ No newline at end of file diff --git a/src/react/reactquestion_file.tsx b/src/react/reactquestion_file.tsx index 7a4c63b4d7..4d6ae021c1 100644 --- a/src/react/reactquestion_file.tsx +++ b/src/react/reactquestion_file.tsx @@ -7,7 +7,7 @@ import { ReactQuestionFactory } from "./reactquestion_factory"; import { attachKey2click } from "./reactSurvey"; import { LoadingIndicatorComponent } from "./components/loading-indicator"; import { SurveyAction } from "./components/action-bar/action-bar-item"; -import { SurveyFileChooseButton } from "../entries/react-ui-model"; +import { ReactElementFactory, SurveyFileChooseButton } from "../entries/react-ui-model"; export class SurveyQuestionFile extends SurveyQuestionElementBase { constructor(props: any) { @@ -112,23 +112,8 @@ export class SurveyQuestionFile extends SurveyQuestionElementBase { ) : null; } - protected renderFileSign(className: string, val: any): JSX.Element | null { - if(!className || !val.name) return null; - return ( -
- { - this.question.doDownloadFile(event, val); - }} - title={val.name} - download={val.name} - style={{ width: this.question.imageWidth }} - > - {val.name} - -
- ); + protected renderPreview(): JSX.Element { + return ReactElementFactory.Instance.createElement("sv-file-preview", { question: this.question }); } protected renderLoadingIndicator(): JSX.Element { return
; @@ -141,44 +126,6 @@ export class SurveyQuestionFile extends SurveyQuestionElementBase { ); } - protected renderPreview(): JSX.Element { - var previews = this.question.previewValue.map((val, index) => { - if (!val) return null; - return ( - - {this.renderFileSign(this.question.cssClasses.fileSign, val)} -
- {this.question.canPreviewImage(val) ? ( - File preview - ) : (this.question.cssClasses.defaultImage?( - - ):null)} - {val.name && !this.question.isReadOnly ? ( -
this.question.doRemoveFile(val)}> - - {this.question.removeFileCaption} - - {(this.question.cssClasses.removeFileSvgIconId) ? - (): null } -
- ) : null} -
- {this.renderFileSign(this.question.cssClasses.fileSignBottom, val)} -
- ); - }); - return
{previews}
; - } } ReactQuestionFactory.Instance.registerQuestion("file", props => { diff --git a/src/vue/components/file-actions/file-choose-button.vue b/src/vue/components/file/file-choose-button.vue similarity index 100% rename from src/vue/components/file-actions/file-choose-button.vue rename to src/vue/components/file/file-choose-button.vue diff --git a/src/vue/file-preview.vue b/src/vue/components/file/file-preview.vue similarity index 100% rename from src/vue/file-preview.vue rename to src/vue/components/file/file-preview.vue From 7c46a3c65e2f433bb8055a86d61eb09c35cafef7 Mon Sep 17 00:00:00 2001 From: Dmitry Kuzin Date: Tue, 3 Oct 2023 12:09:13 +0400 Subject: [PATCH 2/2] Fix angular build --- packages/survey-angular-ui/src/angular-ui.module.ts | 4 ++-- packages/survey-angular-ui/src/angular-ui.ts | 1 + .../{file-actions => file}/choose-file.component.html | 0 .../{file-actions => file}/choose-file.component.ts | 0 .../{file-actions => file}/file-preview.component.html | 0 .../{file-actions => file}/file-preview.component.ts | 0 6 files changed, 3 insertions(+), 2 deletions(-) rename packages/survey-angular-ui/src/components/{file-actions => file}/choose-file.component.html (100%) rename packages/survey-angular-ui/src/components/{file-actions => file}/choose-file.component.ts (100%) rename packages/survey-angular-ui/src/components/{file-actions => file}/file-preview.component.html (100%) rename packages/survey-angular-ui/src/components/{file-actions => file}/file-preview.component.ts (100%) diff --git a/packages/survey-angular-ui/src/angular-ui.module.ts b/packages/survey-angular-ui/src/angular-ui.module.ts index cdd8a0a8cd..3b0caae86d 100644 --- a/packages/survey-angular-ui/src/angular-ui.module.ts +++ b/packages/survey-angular-ui/src/angular-ui.module.ts @@ -114,8 +114,8 @@ import { MultipleTextRowComponent } from "./questions/multipletextrow.component" import { LoadingIndicatorComponent } from "./angular-ui"; import { CoverComponent } from "./components/cover/cover.component"; import { CoverCellComponent } from "./components/cover/cover-cell.component"; -import { ChooseFileBtn } from "./components/file-actions/choose-file.component"; -import { FilePreviewComponent } from "./components/file-actions/file-preview.component"; +import { ChooseFileBtn } from "./components/file/choose-file.component"; +import { FilePreviewComponent } from "./components/file/file-preview.component"; @NgModule({ declarations: [ diff --git a/packages/survey-angular-ui/src/angular-ui.ts b/packages/survey-angular-ui/src/angular-ui.ts index ab1b024fd4..0ca0df7479 100644 --- a/packages/survey-angular-ui/src/angular-ui.ts +++ b/packages/survey-angular-ui/src/angular-ui.ts @@ -65,6 +65,7 @@ export * from "./panel.component"; export * from "./components/popup/modal-container.component"; export * from "./components/popup/popup.service"; export * from "./components/survey-actions/survey-nav-btn.component"; +export * from "./components/file/file-preview.component"; export * from "./questions/matrix.component"; export * from "./components/svg-icon/svg-icon.component"; export * from "./questions/file.component"; diff --git a/packages/survey-angular-ui/src/components/file-actions/choose-file.component.html b/packages/survey-angular-ui/src/components/file/choose-file.component.html similarity index 100% rename from packages/survey-angular-ui/src/components/file-actions/choose-file.component.html rename to packages/survey-angular-ui/src/components/file/choose-file.component.html diff --git a/packages/survey-angular-ui/src/components/file-actions/choose-file.component.ts b/packages/survey-angular-ui/src/components/file/choose-file.component.ts similarity index 100% rename from packages/survey-angular-ui/src/components/file-actions/choose-file.component.ts rename to packages/survey-angular-ui/src/components/file/choose-file.component.ts diff --git a/packages/survey-angular-ui/src/components/file-actions/file-preview.component.html b/packages/survey-angular-ui/src/components/file/file-preview.component.html similarity index 100% rename from packages/survey-angular-ui/src/components/file-actions/file-preview.component.html rename to packages/survey-angular-ui/src/components/file/file-preview.component.html diff --git a/packages/survey-angular-ui/src/components/file-actions/file-preview.component.ts b/packages/survey-angular-ui/src/components/file/file-preview.component.ts similarity index 100% rename from packages/survey-angular-ui/src/components/file-actions/file-preview.component.ts rename to packages/survey-angular-ui/src/components/file/file-preview.component.ts