Skip to content

Commit

Permalink
[4.x] Support line breaks in Bard inline mode (#8598)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Varga <jason@pixelfear.com>
  • Loading branch information
jacksleight and jasonvarga authored Aug 22, 2023
1 parent 0469449 commit 600efbc
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
13 changes: 12 additions & 1 deletion resources/js/components/fieldtypes/bard/BardFieldtype.vue
Original file line number Diff line number Diff line change
Expand Up @@ -644,9 +644,20 @@ export default {
},
getExtensions() {
let modeExts = this.inputIsInline ? [DocumentInline] : [DocumentBlock, HardBreak];
if (this.config.inline === 'break') {
modeExts.push(HardBreak.extend({
addKeyboardShortcuts() {
return {
...this.parent?.(),
'Enter': () => this.editor.commands.setHardBreak(),
}
},
}));
}
let exts = [
CharacterCount.configure({ limit: this.config.character_limit }),
...(this.inputIsInline ? [DocumentInline] : [DocumentBlock, HardBreak]),
...modeExts,
Dropcursor,
Gapcursor,
History,
Expand Down
3 changes: 3 additions & 0 deletions resources/lang/en/fieldtypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
'bard.config.enable_paste_rules' => 'Enables Markdown-style shortcuts when pasting content.',
'bard.config.fullscreen' => 'Enable toggle for fullscreen mode',
'bard.config.inline' => 'Disable block elements like headings, images, and sets.',
'bard.config.inline.disabled' => 'Disabled',
'bard.config.inline.enabled' => 'Enabled without line breaks',
'bard.config.inline.break' => 'Enabled with line breaks',
'bard.config.link_collections' => 'Entries from these collections will be available in the link selector. Leaving this empty will make all entries available.',
'bard.config.link_noopener' => 'Set `rel="noopener"` on all links.',
'bard.config.link_noreferrer' => 'Set `rel="noreferrer"` on all links.',
Expand Down
9 changes: 8 additions & 1 deletion src/Fieldtypes/Bard.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,14 @@ protected function configFieldItems(): array
'inline' => [
'display' => __('Inline'),
'instructions' => __('statamic::fieldtypes.bard.config.inline'),
'type' => 'toggle',
'type' => 'select',
'cast_booleans' => true,
'options' => [
'false' => __('statamic::fieldtypes.bard.config.inline.disabled'),
'true' => __('statamic::fieldtypes.bard.config.inline.enabled'),
'break' => __('statamic::fieldtypes.bard.config.inline.break'),
],
'default' => false,
],
'toolbar_mode' => [
'display' => __('Toolbar Mode'),
Expand Down
23 changes: 18 additions & 5 deletions tests/Fieldtypes/BardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -879,20 +879,33 @@ public function it_converts_a_queryable_value()
$this->assertEquals([['foo' => 'bar']], (new Bard)->toQueryableValue([['foo' => 'bar']]));
}

/** @test */
public function it_augments_inline_value()
/**
* @test
*
* @dataProvider inlineProvider
*/
public function it_augments_inline_value($config)
{
$data = [
['type' => 'text', 'text' => 'This is inline text with '],
['type' => 'text', 'marks' => [['type' => 'bold']], 'text' => 'bold'],
['type' => 'text', 'text' => ' and '],
['type' => 'text', 'text' => ' and'],
['type' => 'hardBreak'],
['type' => 'text', 'marks' => [['type' => 'italic']], 'text' => 'italic'],
['type' => 'text', 'text' => ' text.'],
];

$expected = 'This is inline text with <strong>bold</strong> and <em>italic</em> text.';
$expected = 'This is inline text with <strong>bold</strong> and<br><em>italic</em> text.';

$this->assertEquals($expected, $this->bard(['inline' => true, 'sets' => null])->augment($data));
$this->assertEquals($expected, $this->bard(['inline' => $config, 'sets' => null])->augment($data));
}

public function inlineProvider()
{
return [
'true' => [true],
'break' => ['break'],
];
}

/** @test */
Expand Down

0 comments on commit 600efbc

Please sign in to comment.