Skip to content

Commit

Permalink
Define name setter for trix-editor elements (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
parisk authored Jul 17, 2024
2 parents 78fb953 + c83ab24 commit 5c0f6d5
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 4 deletions.
13 changes: 11 additions & 2 deletions example/blog/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
from django.contrib import admin

from blog.models import Article
from blog.models import Article, Comment

admin.site.register(Article)

class CommentAdminInline(admin.StackedInline):
model = Comment


class ArticleAdmin(admin.ModelAdmin):
inlines = [CommentAdminInline]


admin.site.register(Article, ArticleAdmin)
50 changes: 50 additions & 0 deletions example/blog/migrations/0005_alter_article_excerpt_comment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Generated by Django 4.2.14 on 2024-07-17 08:14

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import prose.fields


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("blog", "0004_alter_article_excerpt"),
]

operations = [
migrations.AlterField(
model_name="article",
name="excerpt",
field=prose.fields.RichTextField(blank=True, default=""),
),
migrations.CreateModel(
name="Comment",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("body", prose.fields.RichTextField(blank=True, default="")),
(
"article",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="blog.article"
),
),
(
"author",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
),
),
],
),
]
11 changes: 10 additions & 1 deletion example/blog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,17 @@ class Article(models.Model):
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
)
excerpt = RichTextField(blank=True, null=True)
excerpt = RichTextField(blank=True, default="")
body = models.OneToOneField(Document, on_delete=models.CASCADE)

def __str__(self):
return f"{self.title} by {self.author.username}: {self.body}"


class Comment(models.Model):
article = models.ForeignKey(Article, on_delete=models.CASCADE)
author = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
)
body = RichTextField(blank=True, default="")
19 changes: 18 additions & 1 deletion prose/static/prose/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,25 @@ function initializeEditors() {
});
}

/**
* https://github.com/withlogicco/django-prose/issues/100
*/
function patchTrixEditorWithNameSetter() {
Object.defineProperty(window.Trix.elements.TrixEditorElement.prototype, "name", {
get() {
return this.inputElement?.name;
},
set(value) {
this.inputElement.name = value;
},
});
}

// When the DOM is initially loaded
document.addEventListener("DOMContentLoaded", initializeEditors);
document.addEventListener("DOMContentLoaded", () => {
initializeEditors();
patchTrixEditorWithNameSetter();
});

// Export the initializeEditors function so it can be called from other scripts
window.djangoProse = window.djangoProse || {};
Expand Down

0 comments on commit 5c0f6d5

Please sign in to comment.