admin icon indicating copy to clipboard operation
admin copied to clipboard

Allow Generic types for Metas

Open gstvg opened this issue 7 years ago • 5 comments

I tried the following:

type RichText string

func (RichText) ConfigureQorMeta(metaor resource.Metaor) {
	if meta, ok := metaor.(*admin.Meta); ok {
		meta.Type = "rich_editor"
		meta.Config = &admin.RichEditorConfig{}
	}
}

type Post struct {
      gorm.Model
      
      Title string
      Body RichText
}

But the temple func "raw" expects string, e gives me an error.

"raw": func(str string) template.HTML { return template.HTML(utils.HTMLSanitizer.Sanitize(str)) },

gstvg avatar Sep 27 '18 16:09 gstvg

This looks like something I wanted to try next week to unify a lot of repepritive code in our product.

I belive this shoudl work somehow.. Can you share the full error and stacktrace? Or: in which template does it trip over {{ raw ... }} ?

cryptix avatar Sep 28 '18 05:09 cryptix

Here: https://github.com/qor/admin/blob/master/views/metas/form/rich_editor.tmpl

gstvg avatar Oct 01 '18 14:10 gstvg

hmm... that calls it with .Value. I'm not sure what that is in your case but to debug further I'd try to change the "raw" template func to something like this and see.. Maybe you need to implement stringer on your type or need to case the string out of your custom implementation.

func raw(iv interface{}) (template.HTML, error) {
        log.Println("debug raw value:", iv)
	switch v := iv.(type) {
	case string:
		return utils.HTMLSanitizer.Sanitize(v), nil
	default:
		return "", fmt.Errorf("raw: unhandled type: %T", iv)
	}
}

cryptix avatar Oct 01 '18 21:10 cryptix

Thanks for your reply and attention. This specific problem its easy to solve. I opened this issue with a more generic intention. I want to implement not only a type for rich editor, but for other commom types too. See my other issue #151. For example:

These types woul configure the options

type Gender string
type Country string

These types would replace the meta template(add a unity selection on the right of the value, allowing for switching between meter and centimeter for example, and automatically convert the value of the input), valuer(append Unity, default(which can be meter as well) for Distance and meter for Meter) and setter(trim Unity from string),

type Distance uint64
type Meter Distance

gstvg avatar Oct 03 '18 04:10 gstvg

Well, not a beautiful solution, but works:

type RichText string

func (RichText) ConfigureQorMeta(metaor resource.Metaor) {
	if meta, ok := metaor.(*admin.Meta); ok {
		meta.Type = "rich_editor"
		meta.Config = &admin.RichEditorConfig{}
		meta.GetBaseResource().(*admin.Resource).GetAdmin().RegisterFuncMap("raw", func(str interface{}) template.HTML {
			return template.HTML(utils.HTMLSanitizer.Sanitize(utils.ToString(str)))
		},
		)
	}
}

type Post struct {
      gorm.Model
      
      Title string
      Body RichText
}

gstvg avatar Oct 27 '18 01:10 gstvg