我正在使用Javascript将InDesign文件转换为ANF (Apple格式,即JSON)。
在ANF中,每个组件只能有一个段落样式(相当于文本帧)。
在InDesign中,文本帧中可以有更多的段落样式。作为解决办法,可以将多个组件放置在容器组件中,以保持布局正确。为此,当我在InDesign文档中找到一个具有多个段落样式的文本框架时,我创建一个容器组件,然后为每个段落创建一个组件,并将其嵌套在容器中。
如果文本框架中的两个或多个连续段落具有相同的段落样式,则需要将它们分组为一个嵌套组件。我知道这应该比较简单,但是在InDesign中工作的Javascript有点过时,所以没有很多现代的便利。我需要用它来创新。
下面是一个简化的数据示例:
var textContainer = {
role: "container",
components: [
{
textStyle: "style-1",
text: "<p>0</p>",
},
{
textStyle: "style-1",
text: "<p>1</p>",
},
{
textStyle: "style-2",
text: "<p>2</p>",
},
{
textStyle: "style-2",
text: "<p>3</p>",
},
{
textStyle: "style-2",
text: "<p>4</p>",
},
{
textStyle: "style-1",
text: "<p>5</p>",
},
{
textStyle: "style-2",
text: "<p>6</p>",
}
]
}因此,在数组中,有两个位置,其中有两组组件,一个是连续的textStyle。我需要把这些放在一个组件里。所以当我完成时数据应该是这样的..。
var textContainer = {
role: "container",
components: [
{
textStyle: "style-1",
text: "<p>0</p><p>1</p>",
},
{
textStyle: "style-2",
text: "<p>2</p><p>3</p><p>4</p>",
},
{
textStyle: "style-1",
text: "<p>5</p>",
},
{
textStyle: "style-2",
text: "<p>6</p>",
}
]
}本质上应该是[[0,1],[2,3,4],5,6];
我是这样开始的:
var simplifiedComponents = [];
var consecutive = ""
for(i=0;i<textContainer.components.length;i++){
if(i > 0 && i < textContainer.components.length - 1){
if(textContainer.components[i].textStyle == textContainer.components[i+1].textStyle){
textContainer.components[i].text += textContainer.components[i+1].text;
textContainer.components[i+1].text = "";
}
}
}但我被卡住了。
有人知道我如何将我的数据转换成这个优化的版本吗?
再说一遍,我需要在这里使用基本的Javascript,因为我无法使用大多数的内容。
发布于 2020-06-12 17:56:38
var textContainer = {
role: "container",
components: [
{
textStyle: "style-1",
text: "<p>0</p>",
},
{
textStyle: "style-1",
text: "<p>1</p>",
},
{
textStyle: "style-2",
text: "<p>2</p>",
},
{
textStyle: "style-2",
text: "<p>3</p>",
},
{
textStyle: "style-2",
text: "<p>4</p>",
},
{
textStyle: "style-1",
text: "<p>5</p>",
},
{
textStyle: "style-2",
text: "<p>6</p>",
}
]
};
var simplifiedComponents = [];
var consecutive = ""
var components = textContainer.components;
for(var i = 0, l = components.length; i < l; i++){
if (i !== 0 && components[i].textStyle !== components[i - 1].textStyle) {
simplifiedComponents.push({text: consecutive, textStyle:components[i - 1].textStyle});
consecutive = ""
}
consecutive = consecutive += components[i].text;
}
simplifiedComponents.push({text: consecutive, textStyle:components[components.length - 1].textStyle});
console.log(simplifiedComponents);
https://stackoverflow.com/questions/62349652
复制相似问题