Skip to content

Commit

Permalink
Merge pull request #47 from OpJePl44tsm4n/saveAsJson
Browse files Browse the repository at this point in the history
Save field as json proposal
  • Loading branch information
bastihilger authored Jan 9, 2022
2 parents 1c55190 + 4b2a3e5 commit 346a838
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
14 changes: 14 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,20 @@ Tiptap::make('FieldName')

When using `'editHtml'` you can set the theme by using using `htmlTheme()`. The default theme used is "material". You can find all the codemirror themes used [here](https://codemirror.net/demo/theme.html) .

## Save JSON

You can optionally use `saveAsJson` to enable the ability to save the tiptap editor content as JSON in the field

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

## Visibility in index view

Like `Textarea` and `Trix` fields this field is hidden from index views. You can make the content visible by using a [computed field](https://nova.laravel.com/docs/3.0/resources/fields.html#computed-fields).
Expand Down
2 changes: 1 addition & 1 deletion dist/js/field.js

Large diffs are not rendered by default.

17 changes: 16 additions & 1 deletion resources/js/components/FormField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,10 @@ export default {
return this.editor ? this.editor.isActive('table') : false;
}
return false;
},
saveAsJson() {
return this.field.saveAsJson ? this.field.saveAsJson : false;
}
},
Expand Down Expand Up @@ -472,8 +476,19 @@ export default {
this.editor = new Editor({
extensions: extensions,
content: this.value,
onCreate() {
try {
let content = JSON.parse(context.value);
this.commands.setContent(content);
} catch {}
},
onUpdate() {
context.updateValue(this.getHTML());
if (context.saveAsJson) {
let jsonContent = this.getJSON();
context.updateValue(JSON.stringify(jsonContent.content));
} else {
context.updateValue(this.getHTML());
}
},
});
},
Expand Down
20 changes: 20 additions & 0 deletions src/Tiptap.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ class Tiptap extends Field
*/
public $showOnIndex = false;

/**
* Indicates if the field should save it's content as json object.
*
* @var bool
*/
public $saveAsJson = false;

/**
* Set the buttons that should be available in the menu.
*
Expand Down Expand Up @@ -187,6 +194,19 @@ public function placeholderBlocks($placeholderBlocks)
]);
}

/**
* Set setting to save the input as json object.
*
* @param boolean $saveAsJson
* @return $this
*/
public function saveAsJson()
{
return $this->withMeta([
'saveAsJson' => true,
]);
}

public function jsonSerialize()
{
return array_merge(parent::jsonSerialize(), [
Expand Down

0 comments on commit 346a838

Please sign in to comment.