-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA02. Quote insertion.js
More file actions
185 lines (171 loc) · 4.52 KB
/
A02. Quote insertion.js
File metadata and controls
185 lines (171 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const zeroMagnitude = {
magnitude: 0,
unit: 'PT'
};
const paragraphStyle_QUOTE_1 = {
namedStyleType: 'NORMAL_TEXT',
indentStart: { magnitude: 56.692913385826756, unit: 'PT' },
indentEnd: { magnitude: 75.75, unit: 'PT' },
indentFirstLine: { magnitude: 56.692913385826756, unit: 'PT' },
spaceAbove: { magnitude: 18, unit: 'PT' },
spaceBelow: { magnitude: 10, unit: 'PT' },
spacingMode: 'NEVER_COLLAPSE',
alignment: 'START',
borderTop: {
width: {
magnitude: 1.5,
unit: 'PT'
},
padding: {
magnitude: 4,
unit: 'PT'
},
dashStyle: 'SOLID',
color: {
color: {
rgbColor: hexToRGB(styles[ACTIVE_STYLE]['main_heading_font_color'])
}
},
}
};
const paragraphStyle_QUOTE_2 = {
namedStyleType: 'NORMAL_TEXT',
indentStart: { magnitude: 56.692913385826756, unit: 'PT' },
indentEnd: { magnitude: 75.75, unit: 'PT' },
indentFirstLine: { magnitude: 56.692913385826756, unit: 'PT' },
spaceAbove: { magnitude: 10, unit: 'PT' },
spaceBelow: { magnitude: 18, unit: 'PT' },
spacingMode: 'NEVER_COLLAPSE',
alignment: 'END',
borderBottom: {
width: {
magnitude: 1.5,
unit: 'PT'
},
padding: {
magnitude: 4,
unit: 'PT'
},
dashStyle: 'SOLID',
color: {
color: {
rgbColor: hexToRGB(styles[ACTIVE_STYLE]['main_heading_font_color'])
}
},
}
};
const textStyle_QUOTE = {
foregroundColor: {
color: {
rgbColor: hexToRGB(styles[ACTIVE_STYLE]['main_heading_font_color'])
}
},
fontSize: {
magnitude: 14,
unit: 'PT'
},
bold: true,
weightedFontFamily: {
fontFamily: styles[ACTIVE_STYLE]['fontFamily'],
weight: 400
}
};
// insertPullQuote use the function
function addQuoteAPIrequest(requests, text, insertIndex, paragraphStyle, textStyle) {
requests.push(
{
insertText: {
location: {
index: insertIndex
},
text: text
}
}, {
updateParagraphStyle: {
paragraphStyle: paragraphStyle,
range: {
startIndex: insertIndex,
endIndex: insertIndex + text.length
},
fields: formFieldsString(paragraphStyle)
}
}, {
updateTextStyle: {
range: {
startIndex: insertIndex,
endIndex: insertIndex + text.length
},
text_style: textStyle,
fields: formFieldsString(textStyle)
}
}
);
}
function insertPullQuote() {
const ui = DocumentApp.getUi();
const doc = DocumentApp.getActiveDocument();
const documentId = doc.getId();
const cursorPosition = detectCursorPosition(doc, documentId);
if (cursorPosition.status == 'error') {
ui.alert(cursorPosition.message);
return 0;
}
const tableStartIndex = cursorPosition.endIndex;
const numRows = 1;
const numCols = 1;
const requests = [];
requests.push(
{
insertTable: {
rows: numRows,
columns: numCols,
location: { index: tableStartIndex }
}
},
{
updateTableCellStyle: {
tableRange: {
tableCellLocation: {
tableStartLocation: {
index: tableStartIndex + 1
},
},
rowSpan: numRows,
columnSpan: numCols
},
tableCellStyle: {
borderTop: tableStyle_TRANSPERENT_BORDER,
borderBottom: tableStyle_TRANSPERENT_BORDER,
borderLeft: tableStyle_TRANSPERENT_BORDER,
borderRight: tableStyle_TRANSPERENT_BORDER,
paddingTop: zeroMagnitude,
paddingBottom: zeroMagnitude,
paddingLeft: zeroMagnitude,
paddingRight: zeroMagnitude
},
fields: 'borderTop,borderBottom,borderLeft,borderRight,paddingTop,paddingBottom,paddingLeft,paddingRight'
}
}
);
const text1 = '“Pull quote would go here, like this.”\n';
addQuoteAPIrequest(requests, text1, tableStartIndex + 4, paragraphStyle_QUOTE_1, textStyle_QUOTE);
const text2 = '– Author, 2022';
addQuoteAPIrequest(requests, text2, tableStartIndex + 4 + text1.length, paragraphStyle_QUOTE_2, textStyle_QUOTE);
requests.push(
{
deleteNamedRange: {
name: cursorPosition.rangeName
}
},
{
deleteContentRange: {
range: {
startIndex: tableStartIndex - 1,
endIndex: tableStartIndex,
}
}
});
Docs.Documents.batchUpdate({
requests: requests
}, documentId);
}