Skip to content

Commit

Permalink
* Style to json
Browse files Browse the repository at this point in the history
  • Loading branch information
askonev committed Feb 15, 2023
1 parent beba7ec commit af961e4
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
50 changes: 50 additions & 0 deletions js/docx/smoke/api_style/to_json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
builder.CreateFile("docx");
var odoc = Api.GetDocument();
let arr_def_styles = ["Normal",
"No Spacing",
"Heading 1",
"Heading 2",
"Heading 3",
"Heading 4",
"Heading 5",
"Heading 6",
"Heading 7",
"Heading 8",
"Heading 9",
"Title",
"Subtitle",
"Quote",
"Intense Quote",
"List Paragraph",
"Caption",
"Header",
"Footer",
"Footnote text",
"Endnote text"]
var result_json = {}
arr_def_styles.forEach(el => {
let st = odoc.GetStyle(el);
let json = st.ToJSON()
result_json[el] = json;
});
GlobalVariable["JSON"] = JSON.stringify(result_json);
builder.CloseFile();

////////////////////////

builder.CreateFile("docx");
var json = GlobalVariable["JSON"]
var obj_json = JSON.parse(json)
var odoc = Api.GetDocument();
for(let k in obj_json){
let st = Api.FromJSON(obj_json[k]);
let opar = Api.CreateParagraph();
opar.AddText(st.GetName())
opar.SetStyle(st);
odoc.Push(opar)
}
let opar = Api.CreateParagraph();
opar.AddText(json);
odoc.Push(opar)
builder.SaveFile("docx", "StyleToJSON.docx");
builder.CloseFile();
29 changes: 29 additions & 0 deletions lib/doc_builder_static_data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

# module for doc builder static data
module DocBuilderStaticData
# Default style list in the CDE
DEFAULT_STYLES = [
'Normal',
'No Spacing',
'Heading 1',
'Heading 2',
'Heading 3',
'Heading 4',
'Heading 5',
'Heading 6',
'Heading 7',
'Heading 8',
'Heading 9',
'Title',
'Subtitle',
'Quote',
'Intense Quote',
'List Paragraph',
'Caption',
'Header',
'Footer',
'Footnote text',
'Endnote text'
].freeze
end
1 change: 1 addition & 0 deletions lib/doc_builder_testing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
require_relative 'doc_builder_testing/doc_builder_wrapper'
require_relative 'doc_builder_testing/exceptions'
require_relative 'doc_builder_testing/web_doc_builder_wrapper'
require_relative 'doc_builder_static_data'
15 changes: 15 additions & 0 deletions spec/docx/smoke/api_style_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,19 @@
docx = builder.build_and_parse('js/docx/smoke/api_style/set_name.js')
expect(docx.elements[1].properties.table_style.name).to eq('My Custom Style')
end

it 'ApiStyle | ToJSON method' do
docx = builder.build_and_parse('js/docx/smoke/api_style/to_json.js')
json = JSON.parse(docx.elements[22].nonempty_runs[0].text)
DocBuilderStaticData::DEFAULT_STYLES.each_with_index do |style, index|
# TODO: To understand why incorrect names are recorded
next if (style == 'Header') || # in the metadata: Custom_Style 719 styleId: 42
(style == 'Footer') || # in the metadata: Custom_Style 725 styleId: 44
(style == 'Footnote text') || # in the metadata: Normal styleId: styleId => 599
(style == 'Endnote text') # in the metadata: Normal styleId: styleId => 599

expect(docx.elements[index + 1].style.name).to eq(style)
expect(JSON.parse(json[style])['name']).to eq(style)
end
end
end

0 comments on commit af961e4

Please sign in to comment.