简体   繁体   English

从 JSON / CSV 加载内容并使用 basil.js 使文本框适合内容

[英]Loading content from a JSON / CSV and make the textbox fit to the content with basil.js

I'm successfully loading a JSON file into InDesign via basil.js.我通过 basil.js 成功地将 JSON 文件加载到 InDesign 中。

var jsonString = b.loadString("data.json");
var jsonData;

function setup() {

  jsonData = b.JSON.decode(jsonString);

  for (var i = 0; i < jsonData.length; i++) {
    var myText = jsonData[i].text;
    b.text(myText, 100, 10 * i, 100, 20);
  };
}

b.go();

The result looks as expected:结果看起来如预期:

结果看起来如预期: What I'd like to achieve is我想要实现的是

  1. That the text frames automatically fit their height to the content, smth.文本框架自动将它们的高度与内容相匹配,smth。 like fit(FitOptions.FRAME_TO_CONTENT);喜欢fit(FitOptions.FRAME_TO_CONTENT);
  2. That the textBoxes continue until the page bounds and then a new page is added.文本框一直持续到页面边界,然后添加新页面。

Any hints are warmly appreciated.任何提示都受到热烈赞赏。 Cheers!干杯!

for your first problem对于你的第一个问题

That the text frames automatically fit their height to the content, smth.文本框架自动将它们的高度与内容相匹配,smth。 like fit(FitOptions.FRAME_TO_CONTENT);喜欢适合(FitOptions.FRAME_TO_CONTENT);

you can just use myText.fit(FitOptions.FRAME_TO_CONTENT);你可以只使用myText.fit(FitOptions.FRAME_TO_CONTENT); . . You don't have to stick with Basil if something is not implemented there.如果那里没有实现某些东西,你不必坚持使用 Basil。

That the textBoxes continue until the page bounds and then a new page is added.文本框一直持续到页面边界,然后添加新页面。

This is a little trickier.这有点棘手。 If you extend the textframes their size will change.如果您扩展文本框,它们的大小将会改变。 You will need another variable outside of your loop that tracks the geometricBounds of the last frame and adds the next frame at the beginning of them.您将需要循环之外的另一个变量来跟踪最后一帧的geometricBounds边界并在它们的开头添加下一帧。 When your last bounds exceed the pageHeight you will need to add a new page.当您的最后一个界限超过 pageHeight 时,您将需要添加一个新页面。

This is something to get you started.这是让你开始的东西。 It is untested code.这是未经测试的代码。

/**
 * InDesign Objects have a property called 
 * geometricBounds
 * these are the bounds in page coordinates sorted like this
 * [y1, x1, y2, x2]
 * or in words
 * [
 *   y of upper left corner, 
 *   x of upper left corner,
 *   y of lower right corner, 
 *   x of lower right corner
 * ]
 */
var prevGeometricBounds = [100, 10, 100, 10]; 
for (var i = 0; i < jsonData.length; i++) {
  var myText = jsonData[i].text;
  var x = prevGeometricBounds[1];
  var y = prevGeometricBounds[0];
  var w = prevGeometricBounds[3] - prevGeometricBounds[1];
  var h = prevGeometricBounds[2] - prevGeometricBounds[1];
  var textFrame = text(myText, x, y, w, h);
  textFrame.fit(FitOptions.FRAME_TO_CONTENT);
  prevGeometricBounds = myText.geometricBounds;
  if(prevGeometricBounds[2] > height){
    addPage();
  }
}

Another hint.另一个提示。 As you can see I omitted the b.如您所见,我省略了b. Basil evolved a lot but we did not yet release the version 2. You can grab the latest version from the develop branch over at GitHub. Basil 发展了很多,但我们尚未发布第 2 版。您可以从 GitHub 的开发分支获取最新版本。 The docs for that are located here .相关文档位于此处


Edit 1编辑 1

In Basil 2 you should be able to load the JSON using loadString and then decode it using JSON.parse在 Basil 2 中,您应该能够使用loadString加载 JSON,然后使用JSON.parse对其进行解码

The Basil text function returns a textFrame . Basil 文本 function 返回一个textFrame So the fit function should work.所以fit的 function 应该可以工作。 If you run into bugs we are happy to accept bug reports over at github .如果您遇到错误,我们很乐意在github接受错误报告。


Edit 2编辑 2

The fit was called on the string you loaded from the JSON not on the result of the text() function. fit是在您从 JSON 加载的字符串上调用的,而不是在text() function 的结果上调用的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM