2025-06-08 16:12:37 +08:00
|
|
|
|
// SiYuan - Refactor your thinking
|
|
|
|
|
// Copyright (c) 2020-present, b3log.org
|
|
|
|
|
//
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
package av
|
|
|
|
|
|
2025-06-10 12:23:07 +08:00
|
|
|
|
import (
|
|
|
|
|
"github.com/88250/lute/ast"
|
|
|
|
|
)
|
2025-06-08 16:12:37 +08:00
|
|
|
|
|
2025-07-04 12:00:47 +08:00
|
|
|
|
// LayoutGallery 描述了卡片布局的结构。
|
2025-06-08 16:12:37 +08:00
|
|
|
|
type LayoutGallery struct {
|
2025-06-08 16:54:27 +08:00
|
|
|
|
*BaseLayout
|
2025-06-08 16:12:37 +08:00
|
|
|
|
|
2025-06-17 16:12:38 +08:00
|
|
|
|
CoverFrom CoverFrom `json:"coverFrom"` // 封面来源,0:无,1:内容图,2:资源字段
|
|
|
|
|
CoverFromAssetKeyID string `json:"coverFromAssetKeyID,omitempty"` // 资源字段 ID,CoverFrom 为 2 时有效
|
|
|
|
|
CardAspectRatio CardAspectRatio `json:"cardAspectRatio"` // 卡片宽高比
|
|
|
|
|
CardSize CardSize `json:"cardSize"` // 卡片大小,0:小卡片,1:中卡片,2:大卡片
|
|
|
|
|
FitImage bool `json:"fitImage"` // 是否适应封面图片大小
|
2025-07-30 10:40:04 +08:00
|
|
|
|
DisplayFieldName bool `json:"displayFieldName"` // 是否显示字段名称
|
2025-06-09 16:23:12 +08:00
|
|
|
|
|
2025-07-04 12:00:47 +08:00
|
|
|
|
CardFields []*ViewGalleryCardField `json:"fields"` // 卡片字段
|
2025-06-08 16:12:37 +08:00
|
|
|
|
|
2025-07-02 17:02:40 +08:00
|
|
|
|
// TODO CardIDs 字段已经废弃,计划于 2026 年 6 月 30 日后删除 https://github.com/siyuan-note/siyuan/issues/15194
|
|
|
|
|
//Deprecated
|
|
|
|
|
CardIDs []string `json:"cardIds"` // 卡片 ID,用于自定义排序
|
2025-06-12 17:14:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-06-10 12:23:07 +08:00
|
|
|
|
func NewLayoutGallery() *LayoutGallery {
|
|
|
|
|
return &LayoutGallery{
|
|
|
|
|
BaseLayout: &BaseLayout{
|
2025-06-30 12:50:50 +08:00
|
|
|
|
Spec: 0,
|
|
|
|
|
ID: ast.NewNodeID(),
|
|
|
|
|
ShowIcon: true,
|
2025-06-10 12:23:07 +08:00
|
|
|
|
},
|
2025-06-17 16:12:38 +08:00
|
|
|
|
CoverFrom: CoverFromContentImage,
|
|
|
|
|
CardAspectRatio: CardAspectRatio16_9,
|
|
|
|
|
CardSize: CardSizeMedium,
|
2025-06-10 12:23:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-17 16:12:38 +08:00
|
|
|
|
type CardAspectRatio int
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
CardAspectRatio16_9 CardAspectRatio = iota // 16:9
|
|
|
|
|
CardAspectRatio9_16 // 9:16
|
|
|
|
|
CardAspectRatio4_3 // 4:3
|
|
|
|
|
CardAspectRatio3_4 // 3:4
|
|
|
|
|
CardAspectRatio3_2 // 3:2
|
|
|
|
|
CardAspectRatio2_3 // 2:3
|
|
|
|
|
CardAspectRatio1_1 // 1:1
|
|
|
|
|
)
|
|
|
|
|
|
2025-06-09 16:23:12 +08:00
|
|
|
|
type CardSize int
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
CardSizeSmall CardSize = iota // 小卡片
|
|
|
|
|
CardSizeMedium // 中卡片
|
|
|
|
|
CardSizeLarge // 大卡片
|
|
|
|
|
)
|
|
|
|
|
|
2025-07-04 12:00:47 +08:00
|
|
|
|
// CoverFrom 描述了卡片封面来源的枚举类型。
|
2025-06-08 22:41:18 +08:00
|
|
|
|
type CoverFrom int
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
CoverFromNone CoverFrom = iota // 无封面
|
|
|
|
|
CoverFromContentImage // 内容图
|
|
|
|
|
CoverFromAssetField // 资源字段
|
2025-07-04 10:33:18 +08:00
|
|
|
|
CoverFromContentBlock // 内容块
|
2025-06-08 22:41:18 +08:00
|
|
|
|
)
|
|
|
|
|
|
2025-07-04 12:00:47 +08:00
|
|
|
|
// ViewGalleryCardField 描述了卡片字段的结构。
|
2025-06-08 16:12:37 +08:00
|
|
|
|
type ViewGalleryCardField struct {
|
2025-06-30 15:06:43 +08:00
|
|
|
|
*BaseField
|
2025-06-08 16:12:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-07-04 12:00:47 +08:00
|
|
|
|
// Gallery 描述了卡片视图实例的结构。
|
2025-06-08 16:12:37 +08:00
|
|
|
|
type Gallery struct {
|
2025-06-08 17:22:03 +08:00
|
|
|
|
*BaseInstance
|
|
|
|
|
|
2025-06-17 17:46:32 +08:00
|
|
|
|
CoverFrom CoverFrom `json:"coverFrom"` // 封面来源
|
|
|
|
|
CoverFromAssetKeyID string `json:"coverFromAssetKeyID,omitempty"` // 资源字段 ID,CoverFrom 为 CoverFromAssetField 时有效
|
|
|
|
|
CardAspectRatio CardAspectRatio `json:"cardAspectRatio"` // 卡片宽高比
|
|
|
|
|
CardSize CardSize `json:"cardSize"` // 卡片大小
|
|
|
|
|
FitImage bool `json:"fitImage"` // 是否适应封面图片大小
|
2025-07-30 10:40:04 +08:00
|
|
|
|
DisplayFieldName bool `json:"displayFieldName"` // 是否显示字段名称
|
2025-07-04 12:00:47 +08:00
|
|
|
|
Fields []*GalleryField `json:"fields"` // 卡片字段
|
|
|
|
|
Cards []*GalleryCard `json:"cards"` // 卡片
|
|
|
|
|
CardCount int `json:"cardCount"` // 总卡片数
|
2025-06-08 16:12:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-07-04 12:00:47 +08:00
|
|
|
|
// GalleryCard 描述了卡片实例的结构。
|
2025-06-08 16:12:37 +08:00
|
|
|
|
type GalleryCard struct {
|
|
|
|
|
ID string `json:"id"` // 卡片 ID
|
|
|
|
|
Values []*GalleryFieldValue `json:"values"` // 卡片字段值
|
|
|
|
|
|
2025-06-09 22:10:05 +08:00
|
|
|
|
CoverURL string `json:"coverURL"` // 卡片封面超链接
|
|
|
|
|
CoverContent string `json:"coverContent"` // 卡片封面文本内容
|
2025-06-08 16:12:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-07-04 12:00:47 +08:00
|
|
|
|
// GalleryField 描述了卡片实例字段的结构。
|
2025-06-08 16:12:37 +08:00
|
|
|
|
type GalleryField struct {
|
2025-06-08 17:22:03 +08:00
|
|
|
|
*BaseInstanceField
|
2025-06-08 16:12:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-07-04 12:00:47 +08:00
|
|
|
|
// GalleryFieldValue 描述了卡片字段实例值的结构。
|
2025-06-08 16:12:37 +08:00
|
|
|
|
type GalleryFieldValue struct {
|
2025-06-08 17:22:03 +08:00
|
|
|
|
*BaseValue
|
2025-06-08 16:12:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-06-11 17:39:32 +08:00
|
|
|
|
func (card *GalleryCard) GetID() string {
|
|
|
|
|
return card.ID
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-08 16:12:37 +08:00
|
|
|
|
func (card *GalleryCard) GetBlockValue() (ret *Value) {
|
|
|
|
|
for _, v := range card.Values {
|
|
|
|
|
if KeyTypeBlock == v.ValueType {
|
|
|
|
|
ret = v.Value
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-12 17:14:54 +08:00
|
|
|
|
func (card *GalleryCard) GetValues() (ret []*Value) {
|
|
|
|
|
ret = []*Value{}
|
|
|
|
|
for _, v := range card.Values {
|
|
|
|
|
ret = append(ret, v.Value)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-12 22:21:07 +08:00
|
|
|
|
func (card *GalleryCard) GetValue(keyID string) (ret *Value) {
|
|
|
|
|
for _, value := range card.Values {
|
|
|
|
|
if nil != value.Value && keyID == value.Value.KeyID {
|
|
|
|
|
ret = value.Value
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-12 17:14:54 +08:00
|
|
|
|
func (gallery *Gallery) GetItems() (ret []Item) {
|
|
|
|
|
ret = []Item{}
|
|
|
|
|
for _, card := range gallery.Cards {
|
|
|
|
|
ret = append(ret, card)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (gallery *Gallery) SetItems(items []Item) {
|
|
|
|
|
gallery.Cards = []*GalleryCard{}
|
|
|
|
|
for _, item := range items {
|
|
|
|
|
gallery.Cards = append(gallery.Cards, item.(*GalleryCard))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-09 17:10:22 +08:00
|
|
|
|
func (gallery *Gallery) CountItems() int {
|
|
|
|
|
return len(gallery.Cards)
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-13 10:49:09 +08:00
|
|
|
|
func (gallery *Gallery) GetFields() (ret []Field) {
|
|
|
|
|
ret = []Field{}
|
2025-06-13 10:39:04 +08:00
|
|
|
|
for _, field := range gallery.Fields {
|
2025-06-13 10:49:09 +08:00
|
|
|
|
ret = append(ret, field)
|
2025-06-13 10:39:04 +08:00
|
|
|
|
}
|
2025-06-13 10:49:09 +08:00
|
|
|
|
return ret
|
2025-06-13 10:39:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-07-06 16:54:55 +08:00
|
|
|
|
func (gallery *Gallery) GetField(id string) (ret Field, fieldIndex int) {
|
|
|
|
|
for i, field := range gallery.Fields {
|
2025-07-05 12:11:12 +08:00
|
|
|
|
if field.ID == id {
|
2025-07-06 16:54:55 +08:00
|
|
|
|
return field, i
|
2025-07-05 12:11:12 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-06 16:54:55 +08:00
|
|
|
|
return nil, -1
|
2025-06-08 16:12:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-08-06 11:15:00 +08:00
|
|
|
|
func (gallery *Gallery) GetValue(itemID, keyID string) (ret *Value) {
|
|
|
|
|
for _, card := range gallery.Cards {
|
|
|
|
|
if card.ID == itemID {
|
|
|
|
|
return card.GetValue(keyID)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-05 12:11:12 +08:00
|
|
|
|
func (gallery *Gallery) GetType() LayoutType {
|
|
|
|
|
return LayoutTypeGallery
|
2025-06-08 16:12:37 +08:00
|
|
|
|
}
|