-
-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Adrian Marin <[email protected]>
- Loading branch information
1 parent
8761ee9
commit 622b95d
Showing
12 changed files
with
267 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<template> | ||
<edit-field-wrapper :field="field" :errors="errors" :index="index" :value-slot-full-width="true"> | ||
<VueTrix v-model="editorContent" | ||
:placeholder="field.placeholder" | ||
class="w-full prose prose-sm" | ||
inputId="trixEditor" | ||
/> | ||
</edit-field-wrapper> | ||
</template> | ||
|
||
<script> | ||
import FormField from '@/js/mixins/form-field' | ||
import VueTrix from 'vue-trix' | ||
export default { | ||
mixins: [FormField], | ||
components: { | ||
VueTrix, | ||
}, | ||
data() { | ||
return { | ||
editorContent: this.field.value, | ||
} | ||
}, | ||
methods: { | ||
setInitialValue() { | ||
this.editorContent = this.field.value | ||
}, | ||
getValue() { | ||
return this.editorContent | ||
}, | ||
focus() { | ||
if (this.$refs['field-input']) this.$refs['field-input'].$emit('focus') | ||
}, | ||
}, | ||
} | ||
</script> | ||
|
||
<style> | ||
.trix-content pre { | ||
background-color: #2D3648 !important; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<template> | ||
<show-field-wrapper :field="field" :index="index" :value-slot-full-width="true"> | ||
<div v-if="field.value"> | ||
<div v-if="showTrix" v-html="field.value" class="prose prose-sm"/> | ||
<a href="javascript:void(0);" | ||
v-if="!field.always_show" | ||
:class="buttonStyle" | ||
@click="toggleTrix" | ||
v-text="linkLabel" | ||
/> | ||
</div> | ||
<empty-dash v-else /> | ||
</show-field-wrapper> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
props: ['field', 'index'], | ||
data() { | ||
return { | ||
showTrix: this.field.always_show, | ||
} | ||
}, | ||
computed: { | ||
buttonStyle() { | ||
if (this.showTrix) return 'font-bold inline-block mt-6' | ||
return 'font-bold' | ||
}, | ||
linkLabel() { | ||
return this.showTrix ? 'Hide Content' : 'Show Content' | ||
}, | ||
}, | ||
methods: { | ||
toggleTrix() { | ||
this.showTrix = !this.showTrix | ||
}, | ||
}, | ||
} | ||
</script> | ||
|
||
<style> | ||
.prose { | ||
max-width: none !important; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require_relative 'field' | ||
|
||
module Avo | ||
module Fields | ||
class TrixField < Field | ||
def initialize(name, **args, &block) | ||
@defaults = { | ||
component: 'trix-field', | ||
} | ||
|
||
super(name, **args, &block) | ||
|
||
hide_on :index | ||
|
||
@always_show = args[:always_show].present? ? args[:always_show] : false | ||
end | ||
|
||
def hydrate_field(fields, model, resource, view) | ||
{ | ||
always_show: @always_show | ||
} | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe 'TrixField', type: :system do | ||
describe 'without value' do | ||
let!(:post) { create :post , body: '' } | ||
|
||
context 'show' do | ||
it 'displays the posts empty body (dash)' do | ||
visit "/avo/resources/posts/#{post.id}" | ||
|
||
expect(find_field_element('body')).to have_text empty_dash | ||
end | ||
end | ||
|
||
context 'edit' do | ||
it 'has the posts body label and empty trix editor and placeholder' do | ||
visit "/avo/resources/posts/#{post.id}/edit" | ||
|
||
body_element = find_field_element('body') | ||
|
||
expect(body_element).to have_text 'Body' | ||
|
||
expect(find(:xpath, "//trix-editor[@input='trixEditor']")[:placeholder]).to have_text('Enter text') | ||
expect(find_trix_editor('trixEditor')).to have_text('') | ||
end | ||
|
||
it 'change the posts body text' do | ||
visit "/avo/resources/posts/#{post.id}/edit" | ||
|
||
fill_in_trix_editor 'trixEditor', with: 'Works for us!!!' | ||
|
||
click_on 'Save' | ||
wait_for_loaded | ||
|
||
click_on 'Show Content' | ||
|
||
expect(find_field_value_element('body')).to have_text 'Works for us!!!' | ||
end | ||
end | ||
end | ||
|
||
describe 'with regular value' do | ||
let!(:body) { 'Example trix text.' } | ||
let!(:post) { create :post , body: body } | ||
|
||
context 'show' do | ||
it 'displays the posts body' do | ||
visit "/avo/resources/posts/#{post.id}" | ||
|
||
click_on 'Show Content' | ||
|
||
expect(find_field_value_element('body')).to have_text body | ||
end | ||
end | ||
|
||
context 'edit' do | ||
it 'has the posts body label' do | ||
visit "/avo/resources/posts/#{post.id}/edit" | ||
|
||
body_element = find_field_element('body') | ||
|
||
expect(body_element).to have_text 'Body' | ||
end | ||
|
||
it 'has filled simple text in trix editor' do | ||
visit "/avo/resources/posts/#{post.id}/edit" | ||
|
||
expect(find_trix_editor('trixEditor').value).to eq('<div>' + body + '</div>') | ||
end | ||
|
||
it 'change the posts body trix to another simple text value' do | ||
visit "/avo/resources/posts/#{post.id}/edit" | ||
|
||
fill_in_trix_editor 'trixEditor', with: 'New example!' | ||
|
||
click_on 'Save' | ||
wait_for_loaded | ||
|
||
click_on 'Show Content' | ||
|
||
expect(find_field_value_element('body')).to have_text 'New example!' | ||
end | ||
end | ||
end | ||
|
||
def fill_in_trix_editor(id, with:) | ||
find(:xpath, "//trix-editor[@input='#{id}']").click.set(with) | ||
end | ||
|
||
def find_trix_editor(id) | ||
find(:xpath, "//*[@id='#{id}']", visible: false) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.