-
Notifications
You must be signed in to change notification settings - Fork 12
Should not be able to edit file def instance in code mode #4025
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -16,9 +16,11 @@ import perform from 'ember-concurrency/helpers/perform'; | |||||||||||||
| import FromElseWhere from 'ember-elsewhere/components/from-elsewhere'; | ||||||||||||||
|
|
||||||||||||||
| import { consume, provide } from 'ember-provide-consume-context'; | ||||||||||||||
| import { use, resource } from 'ember-resources'; | ||||||||||||||
| import window from 'ember-window-mock'; | ||||||||||||||
|
|
||||||||||||||
| import startCase from 'lodash/startCase'; | ||||||||||||||
| import { TrackedObject } from 'tracked-built-ins'; | ||||||||||||||
|
|
||||||||||||||
| import { | ||||||||||||||
| LoadingIndicator, | ||||||||||||||
|
|
@@ -30,6 +32,7 @@ import { File } from '@cardstack/boxel-ui/icons'; | |||||||||||||
| import type { CodeRef } from '@cardstack/runtime-common'; | ||||||||||||||
| import { | ||||||||||||||
| isCardDocumentString, | ||||||||||||||
| isCardErrorJSONAPI, | ||||||||||||||
| RealmPaths, | ||||||||||||||
| PermissionsContextName, | ||||||||||||||
| GetCardContextName, | ||||||||||||||
|
|
@@ -57,8 +60,10 @@ import type PlaygroundPanelService from '@cardstack/host/services/playground-pan | |||||||||||||
| import type RealmService from '@cardstack/host/services/realm'; | ||||||||||||||
| import type RecentFilesService from '@cardstack/host/services/recent-files-service'; | ||||||||||||||
| import type SpecPanelService from '@cardstack/host/services/spec-panel-service'; | ||||||||||||||
| import type StoreService from '@cardstack/host/services/store'; | ||||||||||||||
|
|
||||||||||||||
| import type { | ||||||||||||||
| BaseDef, | ||||||||||||||
| CardDef, | ||||||||||||||
| Format, | ||||||||||||||
| CardContext, | ||||||||||||||
|
|
@@ -143,6 +148,7 @@ export default class CodeSubmode extends Component<Signature> { | |||||||||||||
| @service declare private recentFilesService: RecentFilesService; | ||||||||||||||
| @service declare private realm: RealmService; | ||||||||||||||
| @service declare private specPanelService: SpecPanelService; | ||||||||||||||
| @service declare private store: StoreService; | ||||||||||||||
|
|
||||||||||||||
| @tracked private loadFileError: string | null = null; | ||||||||||||||
| @tracked private userHasDismissedURLError = false; | ||||||||||||||
|
|
@@ -594,8 +600,51 @@ export default class CodeSubmode extends Component<Signature> { | |||||||||||||
| this.operatorModeStateService.updateCardPreviewFormat(format); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| private get isNonModuleFile() { | ||||||||||||||
| return !this.isModule && !isCardDocumentString(this.readyFile.content); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| @use private fileDefResource = resource(() => { | ||||||||||||||
| let state = new TrackedObject<{ | ||||||||||||||
| value: BaseDef | undefined; | ||||||||||||||
| isLoading: boolean; | ||||||||||||||
| error: unknown; | ||||||||||||||
| }>({ | ||||||||||||||
| value: undefined, | ||||||||||||||
| isLoading: false, | ||||||||||||||
| error: undefined, | ||||||||||||||
| }); | ||||||||||||||
| if (!this.isNonModuleFile) { | ||||||||||||||
| return state; | ||||||||||||||
| } | ||||||||||||||
| let fileUrl = this.readyFile.url; | ||||||||||||||
| state.isLoading = true; | ||||||||||||||
| (async () => { | ||||||||||||||
| try { | ||||||||||||||
| let result = await this.store.get(fileUrl, { type: 'file-meta' }); | ||||||||||||||
| if (isCardErrorJSONAPI(result)) { | ||||||||||||||
| state.error = result; | ||||||||||||||
| state.value = undefined; | ||||||||||||||
| } else { | ||||||||||||||
| state.value = result as unknown as BaseDef; | ||||||||||||||
| state.error = undefined; | ||||||||||||||
| } | ||||||||||||||
| } catch (e) { | ||||||||||||||
| state.error = e; | ||||||||||||||
| state.value = undefined; | ||||||||||||||
| } finally { | ||||||||||||||
| state.isLoading = false; | ||||||||||||||
| } | ||||||||||||||
| })(); | ||||||||||||||
| return state; | ||||||||||||||
| }); | ||||||||||||||
|
|
||||||||||||||
| private get isFileDefInstance() { | ||||||||||||||
| return this.fileDefResource?.value !== undefined; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| get isReadOnly() { | ||||||||||||||
| return !this.realm.canWrite(this.readyFile.url); | ||||||||||||||
| return !this.realm.canWrite(this.readyFile.url) || this.isFileDefInstance; | ||||||||||||||
|
Comment on lines
646
to
+647
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For writable realms, Useful? React with 👍 / 👎.
|
||||||||||||||
| return !this.realm.canWrite(this.readyFile.url) || this.isFileDefInstance; | |
| return ( | |
| !this.realm.canWrite(this.readyFile.url) || | |
| this.isFileDefInstance || | |
| this.fileDefResource?.isLoading | |
| ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about this existing function? Can it be used here?
boxel/packages/runtime-common/code-ref.ts
Lines 123 to 124 in 8d45ff7