From 053c8305a1ee32e1bcd5e6bae44906976d70d7be Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Mon, 8 Mar 2021 06:04:54 -0800 Subject: [PATCH] Refactor Quill handling as a separate database column --- app/Http/Controllers/RecipeController.php | 2 + app/JsonApi/Schemas/RecipeSchema.php | 2 +- app/Models/Recipe.php | 24 ++-------- ...2020_12_21_215932_create_recipes_table.php | 1 + ...52009_add_description_delta_to_recipes.php | 45 +++++++++++++++++++ resources/views/recipes/edit.blade.php | 13 ++++-- resources/views/recipes/show.blade.php | 16 +++---- 7 files changed, 71 insertions(+), 32 deletions(-) create mode 100644 database/migrations/2021_03_08_052009_add_description_delta_to_recipes.php diff --git a/app/Http/Controllers/RecipeController.php b/app/Http/Controllers/RecipeController.php index eef0b21..2900882 100644 --- a/app/Http/Controllers/RecipeController.php +++ b/app/Http/Controllers/RecipeController.php @@ -154,6 +154,7 @@ class RecipeController extends Controller $input = $request->validate([ 'name' => ['required', 'string'], 'description' => ['nullable', 'string'], + 'description_delta' => ['nullable', 'string'], 'servings' => ['required', 'numeric'], 'time_prep' => ['nullable', 'numeric'], 'time_active' => ['nullable', 'numeric'], @@ -186,6 +187,7 @@ class RecipeController extends Controller $recipe->fill([ 'name' => Str::lower($input['name']), 'description' => $input['description'], + 'description_delta' => $input['description_delta'], 'servings' => (int) $input['servings'], 'weight' => $input['weight'], 'time_prep' => (int) $input['time_prep'], diff --git a/app/JsonApi/Schemas/RecipeSchema.php b/app/JsonApi/Schemas/RecipeSchema.php index 36b75b8..6ba24a9 100644 --- a/app/JsonApi/Schemas/RecipeSchema.php +++ b/app/JsonApi/Schemas/RecipeSchema.php @@ -28,7 +28,7 @@ class RecipeSchema extends SchemaProvider return [ 'slug' => $resource->slug, 'name' => $resource->name, - 'description' => $resource->description_html, + 'description' => $resource->description, 'time_prep' => $resource->time_prep, 'time_active' => $resource->time_active, 'time_total' => $resource->time_total, diff --git a/app/Models/Recipe.php b/app/Models/Recipe.php index f8b7e06..39bed17 100644 --- a/app/Models/Recipe.php +++ b/app/Models/Recipe.php @@ -62,7 +62,8 @@ use Spatie\Tags\HasTags; * @method static \Illuminate\Database\Eloquent\Builder|Recipe whereTimeActive($value) * @method static \Illuminate\Database\Eloquent\Builder|Recipe whereTimePrep($value) * @method static \Illuminate\Database\Eloquent\Builder|Recipe withUniqueSlugConstraints(\Illuminate\Database\Eloquent\Model $model, string $attribute, array $config, string $slug) - * @property-read string $description_html + * @property string|null $description_delta + * @method static \Illuminate\Database\Eloquent\Builder|Recipe whereDescriptionDelta($value) */ final class Recipe extends Model { @@ -81,6 +82,7 @@ final class Recipe extends Model protected $fillable = [ 'name', 'description', + 'description_delta', 'time_prep', 'time_active', 'source', @@ -114,7 +116,6 @@ final class Recipe extends Model * @inheritdoc */ protected $appends = [ - 'description_html', 'serving_weight', 'time_total', ]; @@ -127,30 +128,13 @@ final class Recipe extends Model return [ 'name' => $this->name, 'tags' => $this->tags->pluck('name')->toArray(), - 'description' => $this->description_html, + 'description' => $this->description, 'source' => $this->source, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, ]; } - /** - * Get description as an HTML string. - */ - public function getDescriptionHtmlAttribute(): ?string { - $description = $this->description; - if (!empty($description)) { - try { - $quill = new Render($this->description); - $description = $quill->render(); - } catch (\Exception $e) { - // TODO: Log this or something. - $description = null; - } - } - return $description; - } - /** * Get total recipe time. */ diff --git a/database/migrations/2020_12_21_215932_create_recipes_table.php b/database/migrations/2020_12_21_215932_create_recipes_table.php index 3c29b07..0599db6 100644 --- a/database/migrations/2020_12_21_215932_create_recipes_table.php +++ b/database/migrations/2020_12_21_215932_create_recipes_table.php @@ -18,6 +18,7 @@ class CreateRecipesTable extends Migration $table->string('name'); $table->string('slug')->unique(); $table->longText('description')->nullable(); + $table->longText('description_delta')->nullable(); $table->integer('time_prep')->nullable(); $table->integer('time_active')->nullable(); $table->string('source')->nullable(); diff --git a/database/migrations/2021_03_08_052009_add_description_delta_to_recipes.php b/database/migrations/2021_03_08_052009_add_description_delta_to_recipes.php new file mode 100644 index 0000000..5789dbb --- /dev/null +++ b/database/migrations/2021_03_08_052009_add_description_delta_to_recipes.php @@ -0,0 +1,45 @@ +longText('description_delta')->nullable()->after('description'); + }); + + foreach (Recipe::all() as $recipe) { + if (empty($recipe->description)) { + continue; + } + + // Format as a basic Quill Delta. + // See: https://quilljs.com/docs/delta/ + $delta = ['ops' => [['insert' => "{$recipe->description}\n"]]]; + $recipe->description_delta = json_encode($delta); + $recipe->save(); + } + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('recipes', function (Blueprint $table) { + $table->dropColumn('description_delta'); + }); + } +} diff --git a/resources/views/recipes/edit.blade.php b/resources/views/recipes/edit.blade.php index db4dc98..234431a 100644 --- a/resources/views/recipes/edit.blade.php +++ b/resources/views/recipes/edit.blade.php @@ -76,6 +76,10 @@ :value="old('description', $recipe->description)" />
+ + @@ -152,7 +156,7 @@ theme: 'snow' }); try { - description.setContents(JSON.parse(document.querySelector('input[name="description"]').value)); + description.setContents(JSON.parse(document.querySelector('input[name="description_delta"]').value)); } catch (e) {} new Draggable.Sortable(document.querySelector('.ingredients'), { @@ -195,8 +199,11 @@ // Remove any hidden templates before form submit. document.querySelectorAll(':scope .entry-template').forEach(e => e.remove()); - // Add description value to hidden field. - document.querySelector('input[name="description"]').value = JSON.stringify(description.getContents()); + // Add description values to hidden fields. + document.querySelector('input[name="description_delta"]').value = JSON.stringify(description.getContents()); + document.querySelector('input[name="description"]').value = description.root.innerHTML + // Remove extraneous spaces from rendered result. + .replaceAll('


', ''); } @endpush diff --git a/resources/views/recipes/show.blade.php b/resources/views/recipes/show.blade.php index 59db811..314493e 100644 --- a/resources/views/recipes/show.blade.php +++ b/resources/views/recipes/show.blade.php @@ -20,15 +20,9 @@
- @if($recipe->description_html) + @if($recipe->description)
- {!! $recipe->description_html !!} -
- @endif - @if(!$recipe->tags->isEmpty()) -
-

Tags:

- {{ implode(', ', $recipe->tags->pluck('name')->all()) }} + {!! $recipe->description !!}
@endif @if($recipe->time_total > 0) @@ -138,4 +132,10 @@ @endif @endif + @if(!$recipe->tags->isEmpty()) +
+

Tags:

+ {{ implode(', ', $recipe->tags->pluck('name')->all()) }} +
+ @endif