mavis-ppt-map.cjs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. const fs = require('fs');
  2. const path = require('path');
  3. const DEFAULT_LAYOUT = Object.freeze({
  4. name: 'LAYOUT_16x9',
  5. widthIn: 10,
  6. heightIn: 5.625,
  7. });
  8. function finiteNumber(value) {
  9. return typeof value === 'number' && Number.isFinite(value) ? value : null;
  10. }
  11. function round(value) {
  12. return Math.round(value * 10000) / 10000;
  13. }
  14. function requireString(value, field) {
  15. if (typeof value !== 'string' || !value.trim()) {
  16. throw new Error(`Missing required PPT element map field: ${field}`);
  17. }
  18. return value.trim();
  19. }
  20. function normalizeLayout(layout) {
  21. const candidate = layout || DEFAULT_LAYOUT;
  22. const widthIn = finiteNumber(candidate.widthIn);
  23. const heightIn = finiteNumber(candidate.heightIn);
  24. if (!widthIn || !heightIn || widthIn <= 0 || heightIn <= 0) {
  25. throw new Error('PPT element map layout must include positive widthIn and heightIn');
  26. }
  27. return {
  28. name: typeof candidate.name === 'string' && candidate.name ? candidate.name : DEFAULT_LAYOUT.name,
  29. widthIn,
  30. heightIn,
  31. };
  32. }
  33. function normalizeRect(input, layout) {
  34. const x = finiteNumber(input && input.x);
  35. const y = finiteNumber(input && input.y);
  36. const w = finiteNumber(input && input.w);
  37. const h = finiteNumber(input && input.h);
  38. if (x === null || y === null || w === null || h === null) {
  39. throw new Error('PPT element map rect must include numeric x, y, w, and h');
  40. }
  41. if (w < 0 || h < 0) {
  42. throw new Error('PPT element map rect width and height must be non-negative');
  43. }
  44. const rect = {
  45. x: round(x),
  46. y: round(y),
  47. w: round(w),
  48. h: round(h),
  49. };
  50. if (x < 0 || y < 0 || x + w > layout.widthIn || y + h > layout.heightIn) {
  51. throw new Error(
  52. `PPT element map rect is outside ${layout.name} bounds: ${JSON.stringify(rect)}`,
  53. );
  54. }
  55. return rect;
  56. }
  57. function toNormalizedRect(rect, layout = DEFAULT_LAYOUT) {
  58. const normalizedLayout = normalizeLayout(layout);
  59. const normalizedRect = normalizeRect(rect, normalizedLayout);
  60. return {
  61. x: round(normalizedRect.x / normalizedLayout.widthIn),
  62. y: round(normalizedRect.y / normalizedLayout.heightIn),
  63. w: round(normalizedRect.w / normalizedLayout.widthIn),
  64. h: round(normalizedRect.h / normalizedLayout.heightIn),
  65. };
  66. }
  67. function compactObject(input) {
  68. return Object.fromEntries(
  69. Object.entries(input).filter(([, value]) => value !== undefined && value !== null && value !== ''),
  70. );
  71. }
  72. function pickStyle(options) {
  73. if (!options || typeof options !== 'object') return undefined;
  74. const keys = [
  75. 'fontSize',
  76. 'fontFace',
  77. 'color',
  78. 'bold',
  79. 'italic',
  80. 'underline',
  81. 'align',
  82. 'valign',
  83. 'margin',
  84. 'fit',
  85. 'fill',
  86. 'line',
  87. 'transparency',
  88. ];
  89. const style = {};
  90. for (const key of keys) {
  91. if (options[key] !== undefined) style[key] = options[key];
  92. }
  93. return Object.keys(style).length ? style : undefined;
  94. }
  95. function createElementMapRecorder(config = {}) {
  96. const layout = normalizeLayout(config.layout);
  97. const artifact = compactObject({
  98. fileName: config.fileName || (config.filePath ? path.basename(config.filePath) : undefined),
  99. filePath: config.filePath,
  100. createdAtMs: config.createdAtMs || Date.now(),
  101. generator: config.generator || 'presentations-skill',
  102. });
  103. const slides = [];
  104. const elementIds = new Set();
  105. let currentSlide = null;
  106. function ensureSlide() {
  107. if (!currentSlide) {
  108. return startSlide({ slideNumber: slides.length + 1, slideId: `slide-${slides.length + 1}` });
  109. }
  110. return currentSlide;
  111. }
  112. function startSlide(slideConfig = {}) {
  113. const slideNumber =
  114. finiteNumber(slideConfig.slideNumber) && slideConfig.slideNumber > 0
  115. ? Math.floor(slideConfig.slideNumber)
  116. : slides.length + 1;
  117. const slide = compactObject({
  118. slideNumber,
  119. slideId: slideConfig.slideId || `slide-${String(slideNumber).padStart(2, '0')}`,
  120. title: slideConfig.title,
  121. chapterId: slideConfig.chapterId,
  122. sectionId: slideConfig.sectionId,
  123. elements: [],
  124. });
  125. slides.push(slide);
  126. currentSlide = slide;
  127. return slide;
  128. }
  129. function recordElement(input = {}) {
  130. const slide = ensureSlide();
  131. const elementId = requireString(input.elementId || input.id, 'elementId');
  132. if (elementIds.has(elementId)) {
  133. throw new Error(`Duplicate PPT element map elementId: ${elementId}`);
  134. }
  135. elementIds.add(elementId);
  136. const rectIn = normalizeRect(input.rectIn || input, layout);
  137. const element = compactObject({
  138. elementId,
  139. kind: requireString(input.kind, 'kind'),
  140. role: input.role,
  141. text: input.text,
  142. rectIn,
  143. normalizedRect: toNormalizedRect(rectIn, layout),
  144. style: input.style,
  145. source: input.source,
  146. pptShapeId: input.pptShapeId,
  147. });
  148. slide.elements.push(element);
  149. return element;
  150. }
  151. function toJSON() {
  152. return {
  153. schema: 'mavis.ppt_element_map.v1',
  154. artifact,
  155. layout,
  156. slides,
  157. };
  158. }
  159. function writeFile(filePath) {
  160. const defaultPath = artifact.filePath
  161. ? artifact.filePath.replace(/(?:\.pptx)?$/i, '.mavis-ppt-map.json')
  162. : '';
  163. const targetPath = filePath || defaultPath;
  164. if (!targetPath) {
  165. throw new Error('PPT element map writeFile requires a target path');
  166. }
  167. fs.mkdirSync(path.dirname(targetPath), { recursive: true });
  168. fs.writeFileSync(targetPath, `${JSON.stringify(toJSON(), null, 2)}\n`, 'utf8');
  169. return targetPath;
  170. }
  171. return {
  172. layout,
  173. startSlide,
  174. recordElement,
  175. toJSON,
  176. writeFile,
  177. };
  178. }
  179. function addMappedText(slide, recorder, input = {}) {
  180. const text = input.text;
  181. const options = {
  182. ...(input.options || {}),
  183. x: input.x,
  184. y: input.y,
  185. w: input.w,
  186. h: input.h,
  187. };
  188. slide.addText(text, options);
  189. if (recorder) {
  190. recorder.recordElement({
  191. elementId: input.elementId || input.id,
  192. kind: 'text',
  193. role: input.role,
  194. text: Array.isArray(text) ? text.map((part) => part.text || '').join('') : String(text || ''),
  195. x: input.x,
  196. y: input.y,
  197. w: input.w,
  198. h: input.h,
  199. style: input.style || pickStyle(input.options),
  200. source: input.source,
  201. });
  202. }
  203. }
  204. function addMappedShape(slide, recorder, shapeType, input = {}) {
  205. const options = {
  206. ...(input.options || {}),
  207. x: input.x,
  208. y: input.y,
  209. w: input.w,
  210. h: input.h,
  211. };
  212. slide.addShape(shapeType, options);
  213. if (recorder) {
  214. recorder.recordElement({
  215. elementId: input.elementId || input.id,
  216. kind: 'shape',
  217. role: input.role,
  218. x: input.x,
  219. y: input.y,
  220. w: input.w,
  221. h: input.h,
  222. style: input.style || pickStyle(input.options),
  223. source: input.source,
  224. });
  225. }
  226. }
  227. function addMappedImage(slide, recorder, input = {}) {
  228. const options = {
  229. ...(input.options || {}),
  230. path: input.path,
  231. data: input.data,
  232. x: input.x,
  233. y: input.y,
  234. w: input.w,
  235. h: input.h,
  236. };
  237. slide.addImage(options);
  238. if (recorder) {
  239. recorder.recordElement({
  240. elementId: input.elementId || input.id,
  241. kind: 'image',
  242. role: input.role,
  243. x: input.x,
  244. y: input.y,
  245. w: input.w,
  246. h: input.h,
  247. style: input.style || pickStyle(input.options),
  248. source: input.source,
  249. });
  250. }
  251. }
  252. module.exports = {
  253. DEFAULT_LAYOUT,
  254. createElementMapRecorder,
  255. toNormalizedRect,
  256. addMappedText,
  257. addMappedShape,
  258. addMappedImage,
  259. };