Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 39 additions & 4 deletions plugins/card-resources/src/components/NewCardForm.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
// limitations under the License.
-->
<script lang="ts">
import { getClient, getCommunicationClient, SpaceSelector } from '@hcengineering/presentation'
import {
getClient,
getCommunicationClient,
SpaceSelector,
DraftController,
draftsStore
} from '@hcengineering/presentation'
import { ModernButton, ModernEditbox, ButtonIcon, IconSend, IconMinimize } from '@hcengineering/ui'
import { createEventDispatcher } from 'svelte'

Expand All @@ -24,16 +30,17 @@
import { getResource } from '@hcengineering/platform'
import { EmptyMarkup, markupToText, isEmptyMarkup, markupToJSON } from '@hcengineering/text'
import { createCard } from '../utils'
import { getCardDraftKey, getEmptyCardDraft, type CardDraft } from '../draft'
import { AttachmentStyledBox } from '@hcengineering/attachment-resources'
import { AttachIcon } from '@hcengineering/text-editor-resources'
import { AttachmentID, BlobParams } from '@hcengineering/communication-types'
import { markupToMarkdown } from '@hcengineering/text-markdown'
import textEditor, { type RefAction } from '@hcengineering/text-editor'
import { defaultMessageInputActions } from '@hcengineering/communication-resources'
import chat from '@hcengineering/chat'
import { Analytics } from '@hcengineering/analytics'

import EditorActions from './EditorActions.svelte'
import { Analytics } from '@hcengineering/analytics'

const dispatch = createEventDispatcher()
const communicationClient = getCommunicationClient()
Expand All @@ -43,12 +50,39 @@
export let type: Ref<MasterTag> = threadMasterTag
export let space: Ref<CardSpace> | undefined = undefined

let title: string = ''
let description = EmptyMarkup
const draftKey = getCardDraftKey()
const draftController = new DraftController<CardDraft>(draftKey)
$: currentDraft = $draftsStore[draftKey] as CardDraft | undefined

let _id = generateId<Card>()
let isExpanded = false
let descriptionBox: AttachmentStyledBox

// Initialize form state from draft or defaults
let title: string = ''
let description: Markup = EmptyMarkup
let initialized = false

function initializeFromDraft (draft: CardDraft): void {
title = draft.title
description = draft.description
if (draft.type !== undefined) {
type = draft.type
}
if (draft.space !== undefined) {
space = draft.space
}
if (draft.title !== '' || !isEmptyMarkup(draft.description)) {
isExpanded = true
}
initialized = true
}

$: if (currentDraft != null && !initialized) {
initializeFromDraft(currentDraft)
}
$: draftController.save({ title, description, type, space }, getEmptyCardDraft())

let creating = false
$: applyDisabled = (title.trim() === '' && isEmptyMarkup(description)) || space == null || type == null || creating

Expand Down Expand Up @@ -137,6 +171,7 @@
title = ''
description = EmptyMarkup
_id = generateId<Card>()
draftController.remove()
}

function expand (): void {
Expand Down
47 changes: 47 additions & 0 deletions plugins/card-resources/src/draft.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright © 2025 Hardcore Engineering Inc.
//
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. You may
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.

import { type Ref, type Markup } from '@hcengineering/core'
import { type CardSpace, type MasterTag } from '@hcengineering/card'
import { EmptyMarkup } from '@hcengineering/text'
import { getCurrentLocation, type Location } from '@hcengineering/ui'

/**
* Card draft interface for DraftController
*/
export interface CardDraft {
title: string
description: Markup
type: Ref<MasterTag> | undefined
space: Ref<CardSpace> | undefined
}

/**
* Generate a location-specific draft key for card drafts
*/
export function getCardDraftKey (location?: Location): string {
const currentLocation = location ?? getCurrentLocation()
const locationKey = currentLocation.path.slice(2).join('.') ?? 'root'
const fragmentKey =
currentLocation.fragment != null && currentLocation.fragment !== '' ? `.${currentLocation.fragment}` : ''
return `card_draft.${locationKey}${fragmentKey}`
}

export function getEmptyCardDraft (): CardDraft {
return {
title: '',
description: EmptyMarkup,
type: undefined,
space: undefined
}
}
Loading