Skip to content

Commit

Permalink
feature: Fill.ToJSON (#1008)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yar04ek authored and askonev committed Dec 8, 2023
1 parent 747ca20 commit 7a1cee5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
29 changes: 29 additions & 0 deletions js/docx/smoke/api_fill/to_json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
builder.CreateFile("docx");
let oDocument = Api.GetDocument();
let oParagraph = oDocument.GetElement(0);
let oGs1 = Api.CreateGradientStop(Api.CreateRGBColor(255, 111, 61), 0);
let oGs2 = Api.CreateGradientStop(Api.CreateRGBColor(255, 213, 191), 100000);
let oFill = Api.CreateRadialGradientFill([oGs1, oGs2]);
let jsonFill = oFill.ToJSON();
oParagraph.AddText(jsonFill);
GlobalVariable["JSON_Fill"] = jsonFill;
builder.CloseFile();

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

builder.CreateFile("docx");
let oDocument = Api.GetDocument();
let oParagraph = oDocument.GetElement(0);
let jsonFill = GlobalVariable["JSON_Fill"];
let oFill = Api.FromJSON(jsonFill);
oParagraph.AddText(jsonFill);
oDocument.Push(oParagraph);
let oParagraph2 = Api.CreateParagraph();
let oStroke = Api.CreateStroke(0, Api.CreateNoFill());
let oDrawing = Api.CreateShape("rect", 1908000, 1404000, oFill, oStroke);
oDrawing.Fill(oFill);
let jsonDrawing = oDrawing.ToJSON(false, true);
oParagraph2.AddText(jsonDrawing);
oDocument.Push(oParagraph2);
builder.SaveFile("docx", "FillToJson.docx");
builder.CloseFile();
13 changes: 13 additions & 0 deletions spec/docx/smoke/api_fill_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,17 @@
docx = builder.build_and_parse('js/docx/smoke/api_fill/get_class_type.js')
expect(docx.elements.first.nonempty_runs[1].text).to eq('Class Type = fill')
end

it 'ApiFill | ToJSON method' do
docx = builder.build_and_parse('js/docx/smoke/api_fill/to_json.js')
json = JSON.parse(docx.elements[1].nonempty_runs.first.text)
fill = json['graphic']['spPr']['fill']['fill']['gsLst'][0]['color']['color']['rgba']
fill1 = json['graphic']['spPr']['fill']['fill']['gsLst'][1]['color']['color']['rgba']
expect(fill['red']).to eq(255)
expect(fill['green']).to eq(111)
expect(fill['blue']).to eq(61)
expect(fill1['red']).to eq(255)
expect(fill1['green']).to eq(213)
expect(fill1['blue']).to eq(191)
end
end

0 comments on commit 7a1cee5

Please sign in to comment.