|
|
@@ -33,12 +33,12 @@ export function getHttpJson(url: string): Promise<{ code: number; data: Record<s
|
|
|
|
|
|
export function jsonToPpt(jsonData: Record<string, any>) {
|
|
|
const slidesStore = useSlidesStore()
|
|
|
- const { width, theme, slides } = jsonData
|
|
|
+ const { width, theme, slides, downType } = jsonData
|
|
|
slidesStore.updateSlideIndex(0)
|
|
|
slidesStore.setViewportSize(width || 1920)
|
|
|
slidesStore.setViewportRatio(theme.viewportRatio || 0.5625)
|
|
|
// 兼容妙极客 这里过滤一下这个数据
|
|
|
- const newSlides = formatSlides(slides)
|
|
|
+ const newSlides = formatSlides(slides, downType)
|
|
|
slidesStore.setTheme(theme)
|
|
|
slidesStore.setSlides(newSlides.length ? newSlides : slidesData)
|
|
|
}
|
|
|
@@ -58,7 +58,7 @@ export function getJsonToBlob() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-function formatSlides(slides: any[]): any[] {
|
|
|
+function formatSlides(slides: any[], downType: string): any[] {
|
|
|
return (slides || []).map(item => {
|
|
|
// 背景兼容 当为渐变并且没有渐变数据的时候,判定为妙极客数据兼容
|
|
|
if (item.background?.type === "gradient") {
|
|
|
@@ -82,6 +82,10 @@ function formatSlides(slides: any[]): any[] {
|
|
|
{ pos: 100, color: (el.gradient.color && el.gradient.color[1]) || "rgba(255,255,255,1)" }
|
|
|
]
|
|
|
}
|
|
|
+ // 计算 兼容ppt导入文字换行
|
|
|
+ if (downType === "pptJsonDownload") {
|
|
|
+ computedFontSize(el)
|
|
|
+ }
|
|
|
}
|
|
|
// ppt 导入的时候 有些数据r:embed 获取不到数据 用emptyImg标记的这些垃圾数据 这里过滤掉
|
|
|
return el.src !== "emptyImg"
|
|
|
@@ -93,3 +97,43 @@ function formatSlides(slides: any[]): any[] {
|
|
|
return item
|
|
|
})
|
|
|
}
|
|
|
+
|
|
|
+function computedFontSize(el: any) {
|
|
|
+ if (el?.text?.content) {
|
|
|
+ const arrFont = getFontSizeAndTextCount(el?.text?.content)
|
|
|
+ if (arrFont.length > 0) {
|
|
|
+ const fontVal = arrFont[0]
|
|
|
+ // 只计算一行的数据,所以当高度大于字体2倍的时候不计算
|
|
|
+ if (el.height && el.width && !(el.height >= fontVal[0] * 2)) {
|
|
|
+ //当宽度大于字体个数的时候也不计算
|
|
|
+ const minFont = Math.max(Math.floor(fontVal[0] / 2), 20) // 一个误差范围
|
|
|
+ const fontNum = arrFont.reduce((total, current) => total + current[1], 0) //里面所有文字加起来
|
|
|
+ if (!(el.width >= fontVal[0] * fontNum + minFont)) {
|
|
|
+ el.width = el.width + fontVal[0] + minFont
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function getFontSizeAndTextCount(htmlString: string) {
|
|
|
+ let result: any[] = []
|
|
|
+ try {
|
|
|
+ const parser = new DOMParser()
|
|
|
+ const doc = parser.parseFromString(htmlString, "text/html")
|
|
|
+ const elements = doc.querySelectorAll('[style*="font-size"]')
|
|
|
+ elements.forEach(element => {
|
|
|
+ const style = element.getAttribute("style")
|
|
|
+ if (style) {
|
|
|
+ const match = style.match(/font-size\s*:\s*([^;\s]+)/i)
|
|
|
+ if (match) {
|
|
|
+ const text = (element.textContent || "").trim()
|
|
|
+ result.push([parseInt(match[1].trim()), text.length, text])
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ } catch (error) {
|
|
|
+ result = []
|
|
|
+ }
|
|
|
+ return result
|
|
|
+}
|