forked from jriou/coller
feat: Add text editor
Use Monaco Editor to create notes. Support syntax highlighting. Store the language with the note in the database to later support syntax highlighting in a note web view. Signed-off-by: Julien Riou <julien@riou.xyz>
This commit is contained in:
parent
b45c3e3253
commit
c54f32495b
9 changed files with 170 additions and 24 deletions
|
@ -20,6 +20,8 @@ type Database struct {
|
|||
expirationInterval int
|
||||
expirations []int
|
||||
expiration int
|
||||
languages []string
|
||||
language string
|
||||
}
|
||||
|
||||
var gconfig = &gorm.Config{
|
||||
|
@ -52,6 +54,8 @@ func NewDatabase(logger *slog.Logger, config *Config) (d *Database, err error) {
|
|||
expirationInterval: config.ExpirationInterval,
|
||||
expirations: config.Expirations,
|
||||
expiration: config.Expiration,
|
||||
languages: internal.ToLowerStringSlice(config.Languages),
|
||||
language: strings.ToLower(config.Language),
|
||||
}
|
||||
|
||||
if err = d.UpdateSchema(); err != nil {
|
||||
|
@ -109,7 +113,7 @@ func (d *Database) Get(id string) (*Note, error) {
|
|||
return nil, nil
|
||||
}
|
||||
|
||||
func (d *Database) Create(content []byte, password string, encrypted bool, expiration int, deleteAfterRead bool) (note *Note, err error) {
|
||||
func (d *Database) Create(content []byte, password string, encrypted bool, expiration int, deleteAfterRead bool, language string) (note *Note, err error) {
|
||||
if expiration == 0 {
|
||||
expiration = d.expiration
|
||||
}
|
||||
|
@ -118,11 +122,21 @@ func (d *Database) Create(content []byte, password string, encrypted bool, expir
|
|||
return nil, fmt.Errorf("invalid expiration: must be one of %s", validExpirations)
|
||||
}
|
||||
|
||||
if language == "" {
|
||||
language = d.language
|
||||
}
|
||||
|
||||
if !slices.Contains(d.languages, language) {
|
||||
validLanguages := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(d.languages)), ", "), "[]")
|
||||
return nil, fmt.Errorf("invalid language: must be one of %s", validLanguages)
|
||||
}
|
||||
|
||||
note = &Note{
|
||||
Content: content,
|
||||
ExpiresAt: time.Now().Add(time.Duration(expiration) * time.Second),
|
||||
Encrypted: encrypted,
|
||||
DeleteAfterRead: deleteAfterRead,
|
||||
Language: language,
|
||||
}
|
||||
if password != "" {
|
||||
if err = internal.ValidatePassword(password); err != nil {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue