Skip to content

Commit

Permalink
fix: <br> and <p> linebreak not correctly rendered
Browse files Browse the repository at this point in the history
  • Loading branch information
MaikoTan committed Sep 21, 2024
1 parent 00c4688 commit 405e83f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 29 deletions.
64 changes: 35 additions & 29 deletions packages/engine-chronocat-api/src/api/message/create/messager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ export class Messager {
}

flush = async () => {
if (this.text) {
this.children.push(r.text(this.text))
this.text = ''
}

if (!this.children.length) return

this.normalize()
Expand All @@ -82,8 +87,9 @@ export class Messager {

private normalize = () => {
this.children = this.children.reduce<O.Partial<RedElement, 'deep'>[]>(
(acc, cur) => {
(acc, cur, idx) => {
const last = acc[acc.length - 1]
const isLastElement = idx === this.children.length - 1
if (
cur.textElement &&
cur.textElement?.content &&
Expand All @@ -93,48 +99,63 @@ export class Messager {
last?.textElement?.atType === AtType.None
) {
last.textElement.content += cur.textElement.content

if (isLastElement) {
last.textElement.content = last.textElement.content.trimEnd()
}
} else {
acc.push(cur)

if (isLastElement && cur.textElement?.content) {
cur.textElement.content = cur.textElement.content.trimEnd()
}
}
return acc
},
[],
)

if (this.children[this.children.length - 1]?.textElement?.content === '\n')
this.children.pop()
return this.children
}

private isEndLine = false
private text = ''
private isHardBreak = false

visit = async (element: h) => {
const { type, attrs, children } = element

if (!['text', 'br', 'p'].includes(type)) {
this.isHardBreak = false
if (this.text) {
this.children.push(r.text(this.text))
this.text = ''
}
}

switch (type) {
case 'text': {
// 文本消息
this.children.push(r.text(attrs['content'] as string))
this.isEndLine = false
this.text += attrs['content'] as string
return
}

case 'br': {
// 换行
if (this.isEndLine) {
this.children.push(r.text('\n'))
}
this.isEndLine = false
this.text += '\n'
this.isHardBreak = true
return
}

case 'p': {
// 文本段落
if (this.isEndLine) {
this.children.push(r.text('\n'))
if (this.isHardBreak) {
this.isHardBreak = false
this.text += '\n'
} else if (!this.text.endsWith('\n')) {
this.text += '\n'
}
await this.render(children)
this.isEndLine = true
this.text += '\n'
return
}

Expand All @@ -143,9 +164,8 @@ export class Messager {
const url = attrs['href'] as string
await this.render(children)
if (url) {
this.children.push(r.text(` ( ${url} )`))
this.text += ` ( ${url} )`
}
this.isEndLine = false
return
}

Expand Down Expand Up @@ -185,7 +205,6 @@ export class Messager {
}

this.children.push(r.remoteImage(result, picType))
this.isEndLine = false
return
}

Expand Down Expand Up @@ -226,7 +245,6 @@ export class Messager {
1,
),
)
this.isEndLine = false

return
} else {
Expand All @@ -246,7 +264,6 @@ export class Messager {
1,
),
)
this.isEndLine = false

return
}
Expand Down Expand Up @@ -290,7 +307,6 @@ export class Messager {
encodeResult.waveAmplitudes,
),
)
this.isEndLine = false

return
} catch (cause) {
Expand Down Expand Up @@ -327,7 +343,6 @@ export class Messager {
1,
),
)
this.isEndLine = false

return
}
Expand All @@ -351,7 +366,6 @@ export class Messager {
fileMime: attrs['chronocat:mime'] as string | undefined,
})
this.children.push(r.remoteFile(result))
this.isEndLine = false
return
}

Expand All @@ -364,8 +378,6 @@ export class Messager {
r.at(this.ctx, attrs['name'] as string, attrs['id'] as string),
)
}

this.isEndLine = false
return
}

Expand Down Expand Up @@ -399,8 +411,6 @@ export class Messager {
),
)
}

this.isEndLine = false
return
}

Expand All @@ -412,8 +422,6 @@ export class Messager {
attrs['key'] as string,
),
)

this.isEndLine = false
return
}

Expand All @@ -439,7 +447,6 @@ export class Messager {
(author?.attrs['id'] as string | undefined) || undefined,
),
)
this.isEndLine = false
return
}

Expand Down Expand Up @@ -488,7 +495,6 @@ export class Messager {
default: {
// 兜底
await this.render(children)
this.isEndLine = false
return
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
import type { RedMessage } from '@chronocat/red'
import h from '@satorijs/element'
import { Messager } from '../../../../src/api/message/create/messager'
Expand Down
1 change: 1 addition & 0 deletions packages/engine-chronocat-api/tests/mocks/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
import type { ChronocatContext } from '@chronocat/shell'
import h from '@satorijs/element'

Expand Down

0 comments on commit 405e83f

Please sign in to comment.