From 7a1cee508fc85905d6bfd5f8b4729df36b28c304 Mon Sep 17 00:00:00 2001 From: Yarosalv Maslov <148754748+Yar04ek@users.noreply.github.com> Date: Tue, 5 Dec 2023 12:35:23 +0100 Subject: [PATCH] feature: Fill.ToJSON (#1008) --- js/docx/smoke/api_fill/to_json.js | 29 +++++++++++++++++++++++++++++ spec/docx/smoke/api_fill_spec.rb | 13 +++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 js/docx/smoke/api_fill/to_json.js diff --git a/js/docx/smoke/api_fill/to_json.js b/js/docx/smoke/api_fill/to_json.js new file mode 100644 index 00000000..4fda3f63 --- /dev/null +++ b/js/docx/smoke/api_fill/to_json.js @@ -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(); diff --git a/spec/docx/smoke/api_fill_spec.rb b/spec/docx/smoke/api_fill_spec.rb index ac5ab389..713c7d1e 100644 --- a/spec/docx/smoke/api_fill_spec.rb +++ b/spec/docx/smoke/api_fill_spec.rb @@ -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