article-content-renderer-vue2 0.5.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +65 -4
- package/dist/index.cjs +6 -6
- package/dist/index.js +2728 -2745
- package/dist/types/types.d.ts +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -502,7 +502,7 @@ export default Vue.extend({
|
|
|
502
502
|
|
|
503
503
|
示例中的 `article-content` 是保存在 JSON 中的固定标识,可以替换为编辑器保存的随机值。选项事件始终返回当前问题的真实 `event.revealKey`,使用方不必预先知道它,也不要假定它等于问题 ID、资源 ID 或目标段落 ID。
|
|
504
504
|
|
|
505
|
-
`option-select` 携带 `{ questionId, resourceId, optionId, revealKey, targetAnchorId? }
|
|
505
|
+
`option-select` 携带 `{ questionId, resourceId, optionId, option, revealKey, targetAnchorId? }`,其中 `option` 是被点击选项的完整属性。组件先记录目标,再发出事件;默认在宿主更新列表、DOM 和布局就绪后定位。可通过下面的 `onAnchorNavigate` 回调控制滚动时机。没有目标的选项仍可解锁,只是不跳转。外部按钮直接解锁时,若没有待定位任务,则只显示内容。
|
|
506
506
|
|
|
507
507
|
### 多个问题与重新隐藏
|
|
508
508
|
|
|
@@ -601,7 +601,7 @@ type OnAnchorNavigate = (
|
|
|
601
601
|
) => void | Promise<void>
|
|
602
602
|
```
|
|
603
603
|
|
|
604
|
-
请求包含 `questionId`、`resourceId`、`optionId`、`revealKey` 和 `targetAnchorId
|
|
604
|
+
请求包含 `questionId`、`resourceId`、`optionId`、`option`、`revealKey` 和 `targetAnchorId`,可通过 `request.option` 读取选项属性。`scrollToAnchor()` 继续使用组件的实例内锚点、`scrollContainer`、`scrollOffset` 和焦点处理。`request.cancel()` 只取消该次点击的定位,不改变解锁列表;也可使用 `renderer-ready` 提供的 `runtime.cancelPendingNavigation()` 取消当前定位。
|
|
605
605
|
|
|
606
606
|
没有绑定锚点或锚点已不存在时,仍发出 `option-select`,但不会调用 `onAnchorNavigate`。同步在 `option-select` 中取消定位时,也不会再调用该次导航回调。每次有效点击只回调一次,重新渲染不会重复回调。
|
|
607
607
|
|
|
@@ -632,9 +632,70 @@ type OnAnchorNavigate = (
|
|
|
632
632
|
|
|
633
633
|
## Events
|
|
634
634
|
|
|
635
|
-
### option-select
|
|
635
|
+
### option-select:点击选项并获取属性
|
|
636
636
|
|
|
637
|
-
|
|
637
|
+
通过 `@option-select="handleOptionSelect"` 注册点击回调,回调参数的 `event.option` 返回当前选项在文章 JSON 中保存的完整属性:
|
|
638
|
+
|
|
639
|
+
```ts
|
|
640
|
+
interface ResourceQuestionSelectEvent {
|
|
641
|
+
questionId: string
|
|
642
|
+
resourceId: string
|
|
643
|
+
optionId: string
|
|
644
|
+
option: Readonly<{
|
|
645
|
+
id: string
|
|
646
|
+
label: string
|
|
647
|
+
targetAnchorId?: string
|
|
648
|
+
}>
|
|
649
|
+
revealKey: string
|
|
650
|
+
targetAnchorId?: string
|
|
651
|
+
}
|
|
652
|
+
```
|
|
653
|
+
|
|
654
|
+
下面的组件接收文章并展示最近点击选项的属性,可在回调内接入自己的业务操作:
|
|
655
|
+
|
|
656
|
+
```vue
|
|
657
|
+
<script lang="ts">
|
|
658
|
+
import Vue, { type PropType } from 'vue'
|
|
659
|
+
import ArticleContentRenderer, {
|
|
660
|
+
type ArticleDocument,
|
|
661
|
+
type ResourceQuestionOption,
|
|
662
|
+
type ResourceQuestionSelectEvent,
|
|
663
|
+
} from 'article-content-renderer-vue2'
|
|
664
|
+
import 'article-content-renderer-vue2/style.css'
|
|
665
|
+
|
|
666
|
+
export default Vue.extend({
|
|
667
|
+
components: { ArticleContentRenderer },
|
|
668
|
+
props: {
|
|
669
|
+
article: { type: Object as PropType<ArticleDocument>, required: true },
|
|
670
|
+
},
|
|
671
|
+
data() {
|
|
672
|
+
return { selectedOption: null as Readonly<ResourceQuestionOption> | null }
|
|
673
|
+
},
|
|
674
|
+
methods: {
|
|
675
|
+
handleOptionSelect(event: ResourceQuestionSelectEvent): void {
|
|
676
|
+
this.selectedOption = event.option
|
|
677
|
+
// event.option.id:选项 ID
|
|
678
|
+
// event.option.label:选项文本
|
|
679
|
+
// event.option.targetAnchorId:目标锚点(未绑定时不存在)
|
|
680
|
+
// event.questionId / event.resourceId:所属问题及资源 ID
|
|
681
|
+
},
|
|
682
|
+
},
|
|
683
|
+
})
|
|
684
|
+
</script>
|
|
685
|
+
|
|
686
|
+
<template>
|
|
687
|
+
<main>
|
|
688
|
+
<ArticleContentRenderer :document="article" @option-select="handleOptionSelect" />
|
|
689
|
+
<pre v-if="selectedOption">{{ JSON.stringify(selectedOption, null, 2) }}</pre>
|
|
690
|
+
</main>
|
|
691
|
+
</template>
|
|
692
|
+
```
|
|
693
|
+
|
|
694
|
+
每次点击触发一次回调,包括重复点击同一选项、没有绑定锚点或锚点已删除的情况;无需在各个 JSON 选项中配置函数。选项通过所属问题和稳定的 `id` 匹配,同名选项也能区分。事件及其 `option` 是冻结的只读快照副本,不会随之后的文章修改而变化;需要编辑时先复制,例如 `{ ...event.option }`。原有的顶层 `optionId`、`targetAnchorId` 等字段继续保留。
|
|
695
|
+
|
|
696
|
+
此示例只展示回调收到的属性。宿主业务允许后可更新 `revealedKeys` 来解锁隐藏内容;默认等待 DOM 和布局后定位可见目标。配置 `onAnchorNavigate` 后,还需使用方显式调用 `request.scrollToAnchor()`。两个 Demo 的解锁面板均会展示 `event.option`。
|
|
697
|
+
|
|
698
|
+
### renderer-ready
|
|
638
699
|
|
|
639
700
|
`renderer-ready` 返回 `ArticleRendererRuntime`,提供 `cancelPendingNavigation()`。Vue 2 函数式组件没有可通过 `ref` 获取的实例,请保存这个运行时句柄,在拒绝、失败或主动取消时调用。
|
|
640
701
|
|