Skip to content
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

richeditor/media_box: add ErrorMessages and Disabled #574

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions media/media_box.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@
Label(field.Label).
Config(cfg).
Disabled(field.Disabled).
Readonly(readonly)
Readonly(readonly).
ErrorMessages(field.Errors...)
}
}

Expand All @@ -114,13 +115,14 @@
}

type QMediaBoxBuilder struct {
fieldName string
label string
value *media_library.MediaBox
config *media_library.MediaBoxConfig
db *gorm.DB
disabled bool
readonly bool
fieldName string
label string
value *media_library.MediaBox
config *media_library.MediaBoxConfig
db *gorm.DB
disabled bool
readonly bool
errorMessages []string
}

func QMediaBox(db *gorm.DB) (r *QMediaBoxBuilder) {
Expand Down Expand Up @@ -160,6 +162,11 @@
return b
}

func (b *QMediaBoxBuilder) ErrorMessages(v ...string) (r *QMediaBoxBuilder) {
b.errorMessages = v
return b
}

func (b *QMediaBoxBuilder) MarshalHTML(c context.Context) (r []byte, err error) {
if b.fieldName == "" {
panic("FieldName required")
Expand All @@ -181,6 +188,15 @@
mediaBoxThumbnails(ctx, b.value, b.fieldName, b.config, b.disabled, b.readonly),
).Name(mediaBoxThumbnailsPortalName(b.fieldName)),
web.Portal().Name(portalName),
h.Iff(len(b.errorMessages) > 0, func() h.HTMLComponent {
var compos []h.HTMLComponent
for _, errMsg := range b.errorMessages {
compos = append(compos, h.Div().Attr("v-pre", true).Text(errMsg))

Check warning on line 194 in media/media_box.go

View check run for this annotation

Codecov / codecov/patch

media/media_box.go#L192-L194

Added lines #L192 - L194 were not covered by tests
}
return h.Div().Class("d-flex flex-column ps-4 py-1 ga-1 text-caption").
ClassIf("text-error", len(b.errorMessages) > 0 && !b.disabled).
ClassIf("text-grey", b.disabled).Children(compos...)

Check warning on line 198 in media/media_box.go

View check run for this annotation

Codecov / codecov/patch

media/media_box.go#L196-L198

Added lines #L196 - L198 were not covered by tests
}),
).Class("pb-4").
Rounded(true),
).MarshalHTML(c)
Expand Down
20 changes: 20 additions & 0 deletions richeditor/redactor/redactor.custom.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
.redactor-toolbar {
z-index: 5;
}

.redactor-styles {
max-height: 50vh;
overflow-y: auto;
}

.redactor-error .redactor-box.redactor-styles-on {
border: 1px solid rgb(var(--v-theme-error));
}

.reactor-disable .redactor-box.redactor-styles-on {
border: 1px solid rgba(var(--v-theme-on-surface), var(--v-high-emphasis-opacity));
}

.redactor-disable-overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.05);
pointer-events: all;
z-index: 6;
}
48 changes: 39 additions & 9 deletions richeditor/richeditor.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@
)

type RichEditorBuilder struct {
db *gorm.DB
name string
value string
label string
placeholder string
plugins []string
setPlugins bool
rawConfig interface{}
db *gorm.DB
name string
value string
label string
placeholder string
plugins []string
setPlugins bool
rawConfig interface{}
errorMessages []string
disabled bool
}

func RichEditor(db *gorm.DB, name string) (r *RichEditorBuilder) {
Expand Down Expand Up @@ -69,6 +71,16 @@
return b
}

func (b *RichEditorBuilder) ErrorMessages(errMsgs ...string) (r *RichEditorBuilder) {
b.errorMessages = errMsgs
return b

Check warning on line 76 in richeditor/richeditor.go

View check run for this annotation

Codecov / codecov/patch

richeditor/richeditor.go#L74-L76

Added lines #L74 - L76 were not covered by tests
}

func (b *RichEditorBuilder) Disabled(disabled bool) (r *RichEditorBuilder) {
b.disabled = disabled
return b

Check warning on line 81 in richeditor/richeditor.go

View check run for this annotation

Codecov / codecov/patch

richeditor/richeditor.go#L79-L81

Added lines #L79 - L81 were not covered by tests
}

func (b *RichEditorBuilder) MarshalHTML(ctx context.Context) ([]byte, error) {
p := Plugins
if b.setPlugins {
Expand All @@ -80,16 +92,34 @@
} else {
redactorB.Config(redactor.Config{Plugins: p})
}

r := h.Components(
v.VSheet(
h.Label(b.label).Class("v-label theme--light"),
redactorB,
h.Div().Style("position:relative").
ClassIf("redactor-error", len(b.errorMessages) > 0 && !b.disabled).
ClassIf("redactor-disable", b.disabled).
Children(
redactorB,
h.Iff(b.disabled, func() h.HTMLComponent {
return h.Div().Class("redactor-disable-overlay")
}),

Check warning on line 106 in richeditor/richeditor.go

View check run for this annotation

Codecov / codecov/patch

richeditor/richeditor.go#L105-L106

Added lines #L105 - L106 were not covered by tests
),
h.Div(
media.QMediaBox(b.db).FieldName(fmt.Sprintf("%s_richeditor_medialibrary", b.name)).
Value(&media_library.MediaBox{}).Config(&media_library.MediaBoxConfig{
AllowType: "image",
}),
).Class("hidden-screen-only"),
h.Iff(len(b.errorMessages) > 0, func() h.HTMLComponent {
var compos []h.HTMLComponent
for _, errMsg := range b.errorMessages {
compos = append(compos, h.Div().Attr("v-pre", true).Text(errMsg))

Check warning on line 117 in richeditor/richeditor.go

View check run for this annotation

Codecov / codecov/patch

richeditor/richeditor.go#L114-L117

Added lines #L114 - L117 were not covered by tests
}
return h.Div().Class("d-flex flex-column ps-4 py-1 ga-1 text-caption").
ClassIf("text-error", len(b.errorMessages) > 0 && !b.disabled).
ClassIf("text-grey", b.disabled).Children(compos...)

Check warning on line 121 in richeditor/richeditor.go

View check run for this annotation

Codecov / codecov/patch

richeditor/richeditor.go#L119-L121

Added lines #L119 - L121 were not covered by tests
}),
).Class("pb-4").Rounded(true).Attr("data-type", "redactor").Attr("style", "position: relative; z-index:1;"),
)
return r.MarshalHTML(ctx)
Expand Down
Loading