mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-09-22 08:30:42 +02:00
186 lines
5.6 KiB
Go
186 lines
5.6 KiB
Go
// 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
|
||
|
||
import (
|
||
"github.com/88250/lute/ast"
|
||
)
|
||
|
||
// LayoutGallery 描述了画廊布局的结构。
|
||
type LayoutGallery struct {
|
||
*BaseLayout
|
||
|
||
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"` // 是否适应封面图片大小
|
||
|
||
CardFields []*ViewGalleryCardField `json:"fields"` // 画廊卡片字段
|
||
CardIDs []string `json:"cardIds"` // 卡片 ID,用于自定义排序
|
||
}
|
||
|
||
func (layoutGallery *LayoutGallery) GetItemIDs() (ret []string) {
|
||
return layoutGallery.CardIDs
|
||
}
|
||
|
||
func NewLayoutGallery() *LayoutGallery {
|
||
return &LayoutGallery{
|
||
BaseLayout: &BaseLayout{
|
||
Spec: 0,
|
||
ID: ast.NewNodeID(),
|
||
ShowIcon: true,
|
||
},
|
||
CoverFrom: CoverFromContentImage,
|
||
CardAspectRatio: CardAspectRatio16_9,
|
||
CardSize: CardSizeMedium,
|
||
}
|
||
}
|
||
|
||
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
|
||
)
|
||
|
||
type CardSize int
|
||
|
||
const (
|
||
CardSizeSmall CardSize = iota // 小卡片
|
||
CardSizeMedium // 中卡片
|
||
CardSizeLarge // 大卡片
|
||
)
|
||
|
||
// CoverFrom 描述了画廊中的卡片封面来源的枚举类型。
|
||
type CoverFrom int
|
||
|
||
const (
|
||
CoverFromNone CoverFrom = iota // 无封面
|
||
CoverFromContentImage // 内容图
|
||
CoverFromAssetField // 资源字段
|
||
)
|
||
|
||
// ViewGalleryCardField 描述了画廊卡片字段的结构。
|
||
type ViewGalleryCardField struct {
|
||
*BaseField
|
||
}
|
||
|
||
// Gallery 描述了画廊实例的结构。
|
||
type Gallery struct {
|
||
*BaseInstance
|
||
|
||
CoverFrom CoverFrom `json:"coverFrom"` // 封面来源
|
||
CoverFromAssetKeyID string `json:"coverFromAssetKeyID,omitempty"` // 资源字段 ID,CoverFrom 为 CoverFromAssetField 时有效
|
||
CardAspectRatio CardAspectRatio `json:"cardAspectRatio"` // 卡片宽高比
|
||
CardSize CardSize `json:"cardSize"` // 卡片大小
|
||
FitImage bool `json:"fitImage"` // 是否适应封面图片大小
|
||
Fields []*GalleryField `json:"fields"` // 画廊字段
|
||
Cards []*GalleryCard `json:"cards"` // 画廊卡片
|
||
CardCount int `json:"cardCount"` // 画廊总卡片数
|
||
}
|
||
|
||
// GalleryCard 描述了画廊实例卡片的结构。
|
||
type GalleryCard struct {
|
||
ID string `json:"id"` // 卡片 ID
|
||
Values []*GalleryFieldValue `json:"values"` // 卡片字段值
|
||
|
||
CoverURL string `json:"coverURL"` // 卡片封面超链接
|
||
CoverContent string `json:"coverContent"` // 卡片封面文本内容
|
||
}
|
||
|
||
// GalleryField 描述了画廊实例卡片字段的结构。
|
||
type GalleryField struct {
|
||
*BaseInstanceField
|
||
}
|
||
|
||
// GalleryFieldValue 描述了画廊实例字段值的结构。
|
||
type GalleryFieldValue struct {
|
||
*BaseValue
|
||
}
|
||
|
||
func (card *GalleryCard) GetID() string {
|
||
return card.ID
|
||
}
|
||
|
||
func (card *GalleryCard) GetBlockValue() (ret *Value) {
|
||
for _, v := range card.Values {
|
||
if KeyTypeBlock == v.ValueType {
|
||
ret = v.Value
|
||
break
|
||
}
|
||
}
|
||
return
|
||
}
|
||
|
||
func (card *GalleryCard) GetValues() (ret []*Value) {
|
||
ret = []*Value{}
|
||
for _, v := range card.Values {
|
||
ret = append(ret, v.Value)
|
||
}
|
||
return
|
||
}
|
||
|
||
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
|
||
}
|
||
|
||
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))
|
||
}
|
||
}
|
||
|
||
func (gallery *Gallery) GetFields() (ret []Field) {
|
||
ret = []Field{}
|
||
for _, field := range gallery.Fields {
|
||
ret = append(ret, field)
|
||
}
|
||
return ret
|
||
}
|
||
|
||
func (gallery *Gallery) GetType() LayoutType {
|
||
return LayoutTypeGallery
|
||
}
|
||
|
||
func (gallery *Gallery) Sort(attrView *AttributeView) {
|
||
sort0(gallery, attrView)
|
||
}
|
||
|
||
func (gallery *Gallery) Filter(attrView *AttributeView) {
|
||
filter0(gallery, attrView)
|
||
}
|