|
| 1 | +// Copyright © 2025 Hardcore Engineering Inc. |
| 2 | +// |
| 3 | +// Licensed under the Eclipse Public License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. You may |
| 5 | +// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0 |
| 6 | +// |
| 7 | +// Unless required by applicable law or agreed to in writing, software |
| 8 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +// |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +import { getClient } from '@hcengineering/presentation' |
| 15 | +import cardPlugin, { type Card, type CardSection } from '@hcengineering/card' |
| 16 | +import { getResource } from '@hcengineering/platform' |
| 17 | +import { type Heading } from '@hcengineering/text-editor' |
| 18 | +import communication from '@hcengineering/communication' |
| 19 | + |
| 20 | +export async function getCardSections (card: Card): Promise<CardSection[]> { |
| 21 | + const client = getClient() |
| 22 | + const sections: CardSection[] = client |
| 23 | + .getModel() |
| 24 | + .findAllSync(cardPlugin.class.CardSection, {}) |
| 25 | + .filter((it) => it._id !== communication.ids.CardMessagesSection) |
| 26 | + .sort((a, b) => a.order - b.order) |
| 27 | + |
| 28 | + const res: CardSection[] = [] |
| 29 | + for (const section of sections) { |
| 30 | + if (section.checkVisibility !== undefined) { |
| 31 | + const isVisibleFn = await getResource(section.checkVisibility) |
| 32 | + const isVisible = await isVisibleFn(card) |
| 33 | + if (isVisible) { |
| 34 | + res.push(section) |
| 35 | + } |
| 36 | + } else { |
| 37 | + res.push(section) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + return res |
| 42 | +} |
| 43 | + |
| 44 | +export function getCardToc (sections: CardSection[], tocBySection: Record<string, Heading[]>): Heading[] { |
| 45 | + const newToc: Heading[] = [] |
| 46 | + |
| 47 | + for (const section of sections) { |
| 48 | + const subToc = tocBySection[section._id] |
| 49 | + if (section.navigation.length > 0) { |
| 50 | + newToc.push( |
| 51 | + ...section.navigation.map((nav) => ({ |
| 52 | + id: nav.id, |
| 53 | + titleIntl: nav.label, |
| 54 | + level: 0, |
| 55 | + group: section._id |
| 56 | + })) |
| 57 | + ) |
| 58 | + } else { |
| 59 | + newToc.push({ |
| 60 | + id: section._id, |
| 61 | + titleIntl: section.label, |
| 62 | + level: 0, |
| 63 | + group: section._id |
| 64 | + }) |
| 65 | + } |
| 66 | + |
| 67 | + if (subToc !== undefined) { |
| 68 | + newToc.push(...subToc.map((it) => ({ ...it, group: section._id }))) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + newToc.push({ |
| 73 | + id: communication.ids.CardMessagesSection, |
| 74 | + titleIntl: communication.string.Messages, |
| 75 | + level: 0, |
| 76 | + group: communication.ids.CardMessagesSection |
| 77 | + }) |
| 78 | + |
| 79 | + return newToc |
| 80 | +} |
0 commit comments