small work on tiptap
This commit is contained in:
parent
f973c59c9a
commit
057868a48e
3 changed files with 48 additions and 21 deletions
|
@ -21,14 +21,14 @@ class ExportItem
|
|||
|
||||
def template_json = template.to_json
|
||||
|
||||
def template_string = TiptapService.new.to_path(template)
|
||||
def template_string = TiptapService.new.to_texts_and_tags(template)
|
||||
|
||||
def path(dossier, attachment: nil, row_index: nil, index: nil)
|
||||
used_tags = TiptapService.used_tags_and_libelle_for(template)
|
||||
substitutions = tags_substitutions(used_tags, dossier, escape: false, memoize: true)
|
||||
substitutions['original-filename'] = attachment.filename.base if attachment
|
||||
|
||||
TiptapService.new.to_path(template, substitutions) + suffix(attachment, row_index, index)
|
||||
TiptapService.new.to_texts_and_tags(template, substitutions) + suffix(attachment, row_index, index)
|
||||
end
|
||||
|
||||
def ==(other)
|
||||
|
|
|
@ -19,10 +19,10 @@ class TiptapService
|
|||
children(node[:content], substitutions, 0)
|
||||
end
|
||||
|
||||
def to_path(node, substitutions = {})
|
||||
def to_texts_and_tags(node, substitutions = {})
|
||||
return '' if node.nil?
|
||||
|
||||
children_path(node[:content], substitutions)
|
||||
children_texts_and_tags(node[:content], substitutions)
|
||||
end
|
||||
|
||||
private
|
||||
|
@ -31,18 +31,24 @@ class TiptapService
|
|||
@body_started = false
|
||||
end
|
||||
|
||||
def children_path(content, substitutions)
|
||||
content.map { node_to_path(_1, substitutions) }.join
|
||||
def children_texts_and_tags(content, substitutions)
|
||||
content.map { node_to_texts_and_tags(_1, substitutions) }.join
|
||||
end
|
||||
|
||||
def node_to_path(node, substitutions)
|
||||
def node_to_texts_and_tags(node, substitutions)
|
||||
case node
|
||||
in type: 'paragraph', content:
|
||||
children_path(content, substitutions)
|
||||
in type: 'text', text:, **rest
|
||||
children_texts_and_tags(content, substitutions)
|
||||
in type: 'paragraph' # empty paragraph
|
||||
''
|
||||
in type: 'text', text:
|
||||
text.strip
|
||||
in type: 'mention', attrs: { id: }, **rest
|
||||
substitutions.fetch(id) { "--#{id}--" }
|
||||
in type: 'mention', attrs: { id:, label: }
|
||||
if substitutions.present?
|
||||
substitutions.fetch(id) { "--#{id}--" }
|
||||
else
|
||||
"<span class='fr-tag fr-tag--sm'>#{label}</span>"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -193,19 +193,40 @@ RSpec.describe TiptapService do
|
|||
end
|
||||
end
|
||||
|
||||
describe '.to_path' do
|
||||
let(:substitutions) { { "dossier_number" => "42" } }
|
||||
let(:json) do
|
||||
{
|
||||
"content" => [
|
||||
{ "type" => "paragraph", "content" => [{ "text" => "export_", "type" => "text" }, { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }, { "text" => " .pdf", "type" => "text" }] }
|
||||
]
|
||||
describe '.to_texts_and_tags' do
|
||||
subject { described_class.new.to_texts_and_tags(json, substitutions) }
|
||||
|
||||
}.deep_symbolize_keys
|
||||
context 'nominal' do
|
||||
let(:json) do
|
||||
{
|
||||
"content" => [
|
||||
{ "type" => "paragraph", "content" => [{ "text" => "export_", "type" => "text" }, { "type" => "mention", "attrs" => { "id" => "dossier_number", "label" => "numéro du dossier" } }, { "text" => " .pdf", "type" => "text" }] }
|
||||
]
|
||||
|
||||
}.deep_symbolize_keys
|
||||
end
|
||||
|
||||
context 'with substitutions' do
|
||||
let(:substitutions) { { "dossier_number" => "42" } }
|
||||
it 'returns texts_and_tags' do
|
||||
is_expected.to eq("export_42.pdf")
|
||||
end
|
||||
end
|
||||
|
||||
context 'without substitutions' do
|
||||
let(:substitutions) { nil }
|
||||
|
||||
it 'returns texts_and_tags' do
|
||||
is_expected.to eq("export_<span class='fr-tag fr-tag--sm'>numéro du dossier</span>.pdf")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it 'returns path' do
|
||||
expect(described_class.new.to_path(json, substitutions)).to eq("export_42.pdf")
|
||||
context 'empty paragraph' do
|
||||
let(:json) { { content: [{ type: 'paragraph' }] } }
|
||||
let(:substitutions) { {} }
|
||||
|
||||
it { is_expected.to eq('') }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue