Skip to content

Commit 90a4d55

Browse files
committed
Refactor article jobs
1 parent 118215f commit 90a4d55

File tree

12 files changed

+165
-42
lines changed

12 files changed

+165
-42
lines changed

app/Http/Controllers/Articles/SeriesController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function store(SeriesRequest $request)
4848
public function edit(Series $series)
4949
{
5050
$this->authorize(SeriesPolicy::UPDATE, $series);
51+
5152
$selectedTags = $series->tags()->pluck('id')->toArray();
5253

5354
return view('series.edit', ['series' => $series, 'tags' => Tag::all(), 'selectedTags' => $selectedTags]);

app/Http/Requests/ArticleRequest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function rules()
1515
'tags' => 'array',
1616
'tags.*' => 'exists:tags,id',
1717
'series' => 'exists:series,id',
18-
'canonical_url' => 'url',
18+
'original_url' => 'url',
1919
];
2020
}
2121

@@ -44,8 +44,8 @@ public function series(): ?string
4444
return $this->get('series');
4545
}
4646

47-
public function canonicalUrl(): ?string
47+
public function originalUrl(): ?string
4848
{
49-
return $this->get('canonical_url');
49+
return $this->get('original_url');
5050
}
5151
}

app/Jobs/CreateArticle.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@ final class CreateArticle
1515

1616
private $author;
1717

18-
private $canonicalUrl;
18+
private $originalUrl;
1919

2020
private $tags;
2121

2222
private $series;
2323

24-
public function __construct(string $title, string $body, User $author, string $canonicalUrl = null, array $tags = [], string $series = null)
24+
public function __construct(string $title, string $body, User $author, array $options = [])
2525
{
2626
$this->title = $title;
2727
$this->body = $body;
2828
$this->author = $author;
29-
$this->canonicalUrl = $canonicalUrl;
30-
$this->tags = $tags;
31-
$this->series = $series;
29+
$this->originalUrl = $options['original_url'] ?? null;
30+
$this->tags = $options['tags'] ?? [];
31+
$this->series = $options['series'] ?? null;
3232
}
3333

3434
public static function fromRequest(ArticleRequest $request): self
@@ -37,9 +37,11 @@ public static function fromRequest(ArticleRequest $request): self
3737
$request->title(),
3838
$request->body(),
3939
$request->author(),
40-
$request->canonicalUrl(),
41-
$request->tags(),
42-
$request->series()
40+
[
41+
'original_url' => $request->originalUrl(),
42+
'tags' => $request->tags(),
43+
'series' => $request->series(),
44+
]
4345
);
4446
}
4547

@@ -48,7 +50,7 @@ public function handle(): Article
4850
$article = new Article([
4951
'title' => $this->title,
5052
'body' => $this->body,
51-
'canonical_url' => $this->canonicalUrl,
53+
'original_url' => $this->originalUrl,
5254
'slug' => $this->title,
5355
]);
5456
$article->authoredBy($this->author);

app/Jobs/UpdateArticle.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@ final class UpdateArticle
1414

1515
private $body;
1616

17-
private $canonicalUrl;
17+
private $originalUrl;
1818

1919
private $tags;
2020

2121
private $series;
2222

23-
public function __construct(Article $article, string $title, string $body, string $canonicalUrl = null, array $tags = [], string $series = null)
23+
public function __construct(Article $article, string $title, string $body, array $options = [])
2424
{
2525
$this->article = $article;
2626
$this->title = $title;
2727
$this->body = $body;
28-
$this->canonicalUrl = $canonicalUrl;
29-
$this->tags = $tags;
30-
$this->series = $series;
28+
$this->originalUrl = $options['original_url'] ?? null;
29+
$this->tags = $options['tags'] ?? [];
30+
$this->series = $options['series'] ?? null;
3131
}
3232

3333
public static function fromRequest(Article $article, ArticleRequest $request): self
@@ -36,9 +36,11 @@ public static function fromRequest(Article $article, ArticleRequest $request): s
3636
$article,
3737
$request->title(),
3838
$request->body(),
39-
$request->canonicalUrl(),
40-
$request->tags(),
41-
$request->series()
39+
[
40+
'original_url' => $request->originalUrl(),
41+
'tags' => $request->tags(),
42+
'series' => $request->series(),
43+
]
4244
);
4345
}
4446

@@ -47,7 +49,7 @@ public function handle(): Article
4749
$this->article->update([
4850
'title' => $this->title,
4951
'body' => $this->body,
50-
'canonical_url' => $this->canonicalUrl,
52+
'original_url' => $this->originalUrl,
5153
'slug' => $this->title,
5254
]);
5355
$this->article->syncTags($this->tags);

app/Models/Article.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ final class Article extends Model
1919
protected $fillable = [
2020
'title',
2121
'body',
22-
'canonical_url',
22+
'original_url',
2323
'slug',
2424
];
2525

@@ -38,9 +38,9 @@ public function body(): string
3838
return $this->body;
3939
}
4040

41-
public function canonicalUrl(): string
41+
public function originalUrl(): string
4242
{
43-
return $this->canonical_url ?: route('articles.show', $this->slug);
43+
return $this->original_url ?: route('articles.show', $this->slug);
4444
}
4545

4646
public function series()

resources/js/editor.js

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Handle the click event of the style buttons inside the editor.
2+
window.handleClick = (style, element) => {
3+
const { styles } = editorConfig();
4+
const input = element.querySelectorAll("textarea")[0];
5+
6+
// Get the start and end positions of the current selection.
7+
const selectionStart = input.selectionStart;
8+
const selectionEnd = input.selectionEnd;
9+
10+
// Find the style in the configuration.
11+
const styleFormat = styles[style];
12+
13+
// Get any prefix and/or suffix characters from the selected style.
14+
const prefix = styleFormat.before ? styleFormat.before : "";
15+
const suffix = styleFormat.after ? styleFormat.after : "";
16+
17+
// Insert the prefix at the relevant position.
18+
input.value = insertCharactersAtPosition(
19+
input.value,
20+
prefix,
21+
selectionStart
22+
);
23+
24+
// Insert the suffix at the relevant position.
25+
input.value = insertCharactersAtPosition(
26+
input.value,
27+
suffix,
28+
selectionEnd + prefix.length
29+
);
30+
31+
// Reselect the selection and focus the input.
32+
input.setSelectionRange(
33+
selectionStart + prefix.length,
34+
selectionEnd + prefix.length
35+
);
36+
input.focus();
37+
}
38+
39+
// Insert provided characters at the desired place in a string.
40+
const insertCharactersAtPosition = (string, character, position) => {
41+
return [
42+
string.slice(0, position),
43+
character,
44+
string.slice(position)
45+
].join("");
46+
}
47+
48+
// Configuration object for the text editor.
49+
window.editorConfig = () => {
50+
return {
51+
styles: {
52+
header: {
53+
before: "### ",
54+
class: {
55+
"fa-header": true
56+
}
57+
},
58+
bold: {
59+
before: "**",
60+
after: "**",
61+
class: {
62+
"fa-bold": true
63+
}
64+
},
65+
italic: {
66+
before: "_",
67+
after: "_",
68+
class: {
69+
"fa-italic": true
70+
}
71+
},
72+
quote: {
73+
before: "> ",
74+
class: {
75+
"fa-quote-left": true
76+
}
77+
},
78+
code: {
79+
before: "`",
80+
after: "`",
81+
class: {
82+
"fa-code": true
83+
}
84+
},
85+
link: {
86+
before: "[](",
87+
after: ")",
88+
class: {
89+
"fa-link": true
90+
}
91+
},
92+
image: {
93+
before: "![](",
94+
after: ")",
95+
class: {
96+
"fa-file-image-o": true
97+
}
98+
}
99+
}
100+
}
101+
}
102+
103+
window.expand = (event, minHeight) => {
104+
setTimeout(function () {
105+
if (event.target.scrollHeight <= minHeight) {
106+
return;
107+
}
108+
109+
event.target.style.cssText = 'height:auto;';
110+
event.target.style.cssText = 'height:' + event.target.scrollHeight + 'px';
111+
}, 0);
112+
}

resources/views/articles/_form.blade.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,17 @@ class="form-control"
2626
@error('body')
2727
@endFormGroup
2828

29-
@formGroup('canonical_url')
30-
<label for="canonical_url">Canonical URL</label>
29+
@formGroup('original_url')
30+
<label for="original_url">Original URL</label>
3131
<input
3232
type="text"
33-
name="canonical_url"
34-
id="canonical_url"
35-
value="{{ isset($article) ? $article->canonicalUrl() : null }}"
36-
class="form-control"
37-
required maxlength="60"
33+
name="original_url"
34+
id="original_url"
35+
value="{{ isset($article) ? $article->originalUrl() : null }}"
36+
class="form-control"
3837
/>
3938
<span class="text-gray-600 text-sm">If you have already posted this article on your own site, enter the URL here and the content will be attributed to you.</span>
40-
@error('canonical_url')
39+
@error('original_url')
4140
@endFormGroup
4241

4342
@formGroup('tags')

resources/views/articles/edit.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@title('Write your article')
1+
@title('Edit your article')
22

33
@extends('layouts.default')
44

resources/views/articles/show.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@extends('layouts.default')
22

33
@push('meta')
4-
<link rel="canonical" href="{{ $article->canonicalUrl() }}" />
4+
<link rel="canonical" href="{{ $article->originalUrl() }}" />
55
@endpush
66

77
@section('content')

tests/Feature/ArticleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public function canonical_urls_are_rendered()
165165
/** @test */
166166
public function custom_canonical_urls_are_rendered()
167167
{
168-
factory(Article::class)->create(['slug' => 'my-first-article', 'canonical_url' => 'https://joedixon.co.uk/my-first-article']);
168+
factory(Article::class)->create(['slug' => 'my-first-article', 'original_url' => 'https://joedixon.co.uk/my-first-article']);
169169

170170
$this->get('/articles/my-first-article')
171171
->see('<link rel="canonical" href="https://joedixon.co.uk/my-first-article" />');

0 commit comments

Comments
 (0)