documents.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. var _ = require("underscore");
  2. var types = exports.types = {
  3. document: "document",
  4. paragraph: "paragraph",
  5. run: "run",
  6. text: "text",
  7. tab: "tab",
  8. hyperlink: "hyperlink",
  9. noteReference: "noteReference",
  10. image: "image",
  11. note: "note",
  12. commentReference: "commentReference",
  13. comment: "comment",
  14. table: "table",
  15. tableRow: "tableRow",
  16. tableCell: "tableCell",
  17. "break": "break",
  18. bookmarkStart: "bookmarkStart"
  19. };
  20. function Document(children, options) {
  21. options = options || {};
  22. return {
  23. type: types.document,
  24. children: children,
  25. notes: options.notes || new Notes({}),
  26. comments: options.comments || []
  27. };
  28. }
  29. function Paragraph(children, properties) {
  30. properties = properties || {};
  31. var indent = properties.indent || {};
  32. return {
  33. type: types.paragraph,
  34. children: children,
  35. styleId: properties.styleId || null,
  36. styleName: properties.styleName || null,
  37. numbering: properties.numbering || null,
  38. alignment: properties.alignment || null,
  39. indent: {
  40. start: indent.start || null,
  41. end: indent.end || null,
  42. firstLine: indent.firstLine || null,
  43. hanging: indent.hanging || null
  44. }
  45. };
  46. }
  47. function Run(children, properties) {
  48. properties = properties || {};
  49. return {
  50. type: types.run,
  51. children: children,
  52. styleId: properties.styleId || null,
  53. styleName: properties.styleName || null,
  54. isBold: !!properties.isBold,
  55. isUnderline: !!properties.isUnderline,
  56. isItalic: !!properties.isItalic,
  57. isStrikethrough: !!properties.isStrikethrough,
  58. isAllCaps: !!properties.isAllCaps,
  59. isSmallCaps: !!properties.isSmallCaps,
  60. verticalAlignment: properties.verticalAlignment || verticalAlignment.baseline,
  61. font: properties.font || null,
  62. fontSize: properties.fontSize || null,
  63. highlight: properties.highlight || null
  64. };
  65. }
  66. var verticalAlignment = {
  67. baseline: "baseline",
  68. superscript: "superscript",
  69. subscript: "subscript"
  70. };
  71. function Text(value) {
  72. return {
  73. type: types.text,
  74. value: value
  75. };
  76. }
  77. function Tab() {
  78. return {
  79. type: types.tab
  80. };
  81. }
  82. function Hyperlink(children, options) {
  83. return {
  84. type: types.hyperlink,
  85. children: children,
  86. href: options.href,
  87. anchor: options.anchor,
  88. targetFrame: options.targetFrame
  89. };
  90. }
  91. function NoteReference(options) {
  92. return {
  93. type: types.noteReference,
  94. noteType: options.noteType,
  95. noteId: options.noteId
  96. };
  97. }
  98. function Notes(notes) {
  99. this._notes = _.indexBy(notes, function(note) {
  100. return noteKey(note.noteType, note.noteId);
  101. });
  102. }
  103. Notes.prototype.resolve = function(reference) {
  104. return this.findNoteByKey(noteKey(reference.noteType, reference.noteId));
  105. };
  106. Notes.prototype.findNoteByKey = function(key) {
  107. return this._notes[key] || null;
  108. };
  109. function Note(options) {
  110. return {
  111. type: types.note,
  112. noteType: options.noteType,
  113. noteId: options.noteId,
  114. body: options.body
  115. };
  116. }
  117. function commentReference(options) {
  118. return {
  119. type: types.commentReference,
  120. commentId: options.commentId
  121. };
  122. }
  123. function comment(options) {
  124. return {
  125. type: types.comment,
  126. commentId: options.commentId,
  127. body: options.body,
  128. authorName: options.authorName,
  129. authorInitials: options.authorInitials
  130. };
  131. }
  132. function noteKey(noteType, id) {
  133. return noteType + "-" + id;
  134. }
  135. function Image(options) {
  136. return {
  137. type: types.image,
  138. // `read` is retained for backwards compatibility, but other read
  139. // methods should be preferred.
  140. read: function(encoding) {
  141. if (encoding) {
  142. return options.readImage(encoding);
  143. } else {
  144. return options.readImage().then(function(arrayBuffer) {
  145. return Buffer.from(arrayBuffer);
  146. });
  147. }
  148. },
  149. readAsArrayBuffer: function() {
  150. return options.readImage();
  151. },
  152. readAsBase64String: function() {
  153. return options.readImage("base64");
  154. },
  155. readAsBuffer: function() {
  156. return options.readImage().then(function(arrayBuffer) {
  157. return Buffer.from(arrayBuffer);
  158. });
  159. },
  160. altText: options.altText,
  161. contentType: options.contentType
  162. };
  163. }
  164. function Table(children, properties) {
  165. properties = properties || {};
  166. return {
  167. type: types.table,
  168. children: children,
  169. styleId: properties.styleId || null,
  170. styleName: properties.styleName || null
  171. };
  172. }
  173. function TableRow(children, options) {
  174. options = options || {};
  175. return {
  176. type: types.tableRow,
  177. children: children,
  178. isHeader: options.isHeader || false
  179. };
  180. }
  181. function TableCell(children, options) {
  182. options = options || {};
  183. return {
  184. type: types.tableCell,
  185. children: children,
  186. colSpan: options.colSpan == null ? 1 : options.colSpan,
  187. rowSpan: options.rowSpan == null ? 1 : options.rowSpan
  188. };
  189. }
  190. function Break(breakType) {
  191. return {
  192. type: types["break"],
  193. breakType: breakType
  194. };
  195. }
  196. function BookmarkStart(options) {
  197. return {
  198. type: types.bookmarkStart,
  199. name: options.name
  200. };
  201. }
  202. exports.document = exports.Document = Document;
  203. exports.paragraph = exports.Paragraph = Paragraph;
  204. exports.run = exports.Run = Run;
  205. exports.text = exports.Text = Text;
  206. exports.tab = exports.Tab = Tab;
  207. exports.Hyperlink = Hyperlink;
  208. exports.noteReference = exports.NoteReference = NoteReference;
  209. exports.Notes = Notes;
  210. exports.Note = Note;
  211. exports.commentReference = commentReference;
  212. exports.comment = comment;
  213. exports.Image = Image;
  214. exports.Table = Table;
  215. exports.TableRow = TableRow;
  216. exports.TableCell = TableCell;
  217. exports.lineBreak = Break("line");
  218. exports.pageBreak = Break("page");
  219. exports.columnBreak = Break("column");
  220. exports.BookmarkStart = BookmarkStart;
  221. exports.verticalAlignment = verticalAlignment;