Skip to content

Commit

Permalink
add RTL support
Browse files Browse the repository at this point in the history
  • Loading branch information
bastihilger committed Jan 10, 2022
1 parent 8406f80 commit 6dd5fa2
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 14 deletions.
15 changes: 15 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ Tiptap::make('FieldName')
'|',
'textAlign',
'|',
'rtl',
'|',
'history',
'|',
'editHtml',
Expand Down Expand Up @@ -172,6 +174,19 @@ Tiptap::make('FieldName')
->defaultAlignment('right'),
```

## RTL support with `rtl`

When adding `rtl` you get a button for toggling RTL mode for all selected block elements (`dir="rtl"`).

``` php
Tiptap::make('FieldName')
->buttons([
'italic',
'bold',
'rtl',
]),
```

## The two different "code" buttons

`'code'` is inline code (like `<code></code>`) while `'codeBlock'` will give you `<pre><code></code></pre>`.
Expand Down
2 changes: 1 addition & 1 deletion dist/js/field.js

Large diffs are not rendered by default.

45 changes: 40 additions & 5 deletions resources/js/components/FormField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ export default {
},
alignments() {
return this.field.alignments ? this.field.alignments : ['left', 'center', 'right', 'justify'];
return this.field.alignments ? this.field.alignments : ['start', 'center', 'end', 'justify'];
},
alignElements() {
Expand Down Expand Up @@ -407,14 +407,49 @@ export default {
Heading.configure({
levels: this.headingLevels,
}).extend({
addAttributes() {
return {
...this.parent?.(),
dir: String,
}
}
}),
Blockquote.extend({
addAttributes() {
return {
...this.parent?.(),
dir: String,
}
}
}),
BulletList.extend({
addAttributes() {
return {
...this.parent?.(),
dir: String,
}
}
}),
Blockquote,
BulletList,
HorizontalRule,
ListItem,
OrderedList,
OrderedList.extend({
addAttributes() {
return {
...this.parent?.(),
dir: String,
}
}
}),
HardBreak,
Paragraph,
Paragraph.extend({
addAttributes() {
return {
...this.parent?.(),
dir: String,
}
}
}),
Table.configure({
resizable: true,
}),
Expand Down
19 changes: 13 additions & 6 deletions resources/js/components/buttons/RtlButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<base-button
:isActive="rtlIsActive()"
:isDisabled="mode != 'editor'"
:clickMethod="toggleRtl"
:clickMethod="setRtl"
:title="'RTL'"
:icon="['fas', 'paragraph-rtl']"
>
Expand All @@ -26,14 +26,21 @@ export default {
rtlIsActive() {
return this.editor ? this.editor.isActive({ dir: 'rtl' }) : false;
},
toggleRtl() {
setRtl() {
const okNodes = [
'paragraph', 'heading'
];
if (this.rtlIsActive()) {
this.editor.chain().focus().updateAttributes('paragraph', { dir: 'auto' }).run();
okNodes.forEach(
nodename => this.editor.chain().focus().updateAttributes(nodename, { dir: 'ltr' }).run()
);
} else {
this.editor.chain().focus().updateAttributes('paragraph', { dir: 'rtl' }).run();
this.editor.chain().focus().updateAttributes('heading', { dir: 'rtl' }).run();
okNodes.forEach(
nodename => this.editor.chain().focus().updateAttributes(nodename, { dir: 'rtl' }).run()
);
}
}
}
}
Expand Down
26 changes: 24 additions & 2 deletions resources/js/components/buttons/TextAlignButtons.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
:clickMethod="setAlignment"
:clickMethodParameters="alignment"
:title="__('align '+alignment)"
:icon="['fas', 'align-'+alignment]"
:icon="['fas', 'align-'+iconName(alignment)]"
>
</base-button>
</span>
Expand All @@ -26,10 +26,32 @@ export default {
},
methods: {
iconName(alignment) {
if (alignment == 'start') { return 'left' };
if (alignment == 'end') { return 'right' };
return alignment;
},
alignmentIsActive(alignment) {
return this.editor ? this.editor.isActive({ textAlign: alignment }) : false;
let isActive = this.editor ? this.editor.isActive({ textAlign: alignment }) : false;
if (alignment == 'left') {
isActive = this.editor
? (this.editor.isActive({ textAlign: 'left' }) || this.editor.isActive({ textAlign: 'start' }))
: false
}
if (alignment == 'right') {
isActive = this.editor
? (this.editor.isActive({ textAlign: 'right' }) || this.editor.isActive({ textAlign: 'end' }))
: false
}
return isActive;
},
setAlignment(alignment) {
if (alignment == 'left') {
alignment = 'start';
}
if (alignment == 'right') {
alignment = 'end';
}
this.editor.chain().focus().setTextAlign(alignment).run();
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/Tiptap.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ public function htmlTheme($htmlTheme)
*/
public function alignments($alignments)
{
$alignments = array_map(function($item){
if($item == 'left') { return 'start'; }
if($item == 'right') { return 'end'; }
return $item;
}, $alignments);

return $this->withMeta([
'alignments' => $alignments,
]);
Expand Down

0 comments on commit 6dd5fa2

Please sign in to comment.