Skip to content
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

Link comments to scenes based on the commentId prop #4795

Merged
merged 5 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { optionalMap } from '../../../../core/shared/optional-utils'
import { useScenes } from '../../../../core/commenting/comment-hooks'
import { safeIndex } from '../../../../core/shared/array-utils'
import * as EP from '../../../../core/shared/element-path'
import { SceneCommentIdPropName } from '../../../../core/model/scene-id-utils'

export function useCommentModeSelectAndHover(comment: CommentId | null): MouseCallbacks {
const dispatch = useDispatch()
Expand Down Expand Up @@ -140,7 +141,10 @@ export function getIdOfScene(scene: ElementInstanceMetadata): string | null {
return null
}

const idProperty = getJSXAttributesAtPath(sceneElement.value.props, create('id'))
const idProperty = getJSXAttributesAtPath(
sceneElement.value.props,
create(SceneCommentIdPropName),
)
const currentValue = optionalMap(jsxSimpleAttributeToValue, idProperty?.attribute)
if (currentValue == null || isLeft(currentValue) || typeof currentValue.value !== 'string') {
return null
Expand Down
3 changes: 2 additions & 1 deletion editor/src/components/canvas/controls/comment-popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { RemixNavigationAtom } from '../remix/utopia-remix-root-component'
import { getIdOfScene } from './comment-mode/comment-mode-hooks'
import { motion, useAnimation } from 'framer-motion'
import type { EditorDispatch } from '../../editor/action-types'
import { SceneCommentIdPropName } from '../../../core/model/scene-id-utils'

export const ComposerEditorClassName = 'lb-composer-editor'

Expand Down Expand Up @@ -175,7 +176,7 @@ const CommentThread = React.memo(({ comment }: CommentThreadProps) => {
: [
setProp_UNSAFE(
scene.elementPath,
create('id'),
create(SceneCommentIdPropName),
jsExpressionValue(sceneId, emptyComments),
),
]
Expand Down
6 changes: 3 additions & 3 deletions editor/src/core/model/scene-id-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import * as PP from '../shared/property-path'
import { assertNever } from '../shared/utils'
import { isSceneAgainstImports, isRemixSceneAgainstImports } from './project-file-utils'

const IdPropName = 'id'
export const SceneCommentIdPropName = 'data-comment-id'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's ONLY for scenes, could we make this just commentId? Scene is our component, so we we can make it a bit less clunky

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, I updated it in 7ebe371.

The reason I went with data-comment-id is because it fit the mold of the other Utopia-specific props on Scene (data-label, data-uid), and using data-comment-id didn't make it necessary to tweak the code of Scene (without the tweaking, a React error is triggered: React does not recognize the 'commentId' prop on a DOM element, since all props on Scene are forwarded to the underlying div).


function getIdPropFromJSXElement(element: JSXElement): string | null {
const maybeIdProp = getJSXAttribute(element.props, IdPropName)
const maybeIdProp = getJSXAttribute(element.props, SceneCommentIdPropName)
if (
maybeIdProp == null ||
maybeIdProp.type !== 'ATTRIBUTE_VALUE' ||
Expand All @@ -28,7 +28,7 @@ function getIdPropFromJSXElement(element: JSXElement): string | null {
function setIdPropOnJSXElement(element: JSXElement, idPropValueToUse: string): JSXElement | null {
const updatedProps = setJSXValueAtPath(
element.props,
PP.create(IdPropName),
PP.create(SceneCommentIdPropName),
jsExpressionValue(idPropValueToUse, emptyComments),
)

Expand Down
6 changes: 5 additions & 1 deletion editor/src/core/third-party/utopia-api-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ const BasicUtopiaSceneDescriptor = (
jsxElementWithoutUID(
name,
[
jsxAttributesEntry('id', jsExpressionValue('scene', emptyComments), emptyComments),
jsxAttributesEntry(
'data-comment-id',
jsExpressionValue('scene', emptyComments),
emptyComments,
),
jsxAttributesEntry('style', styleProp(), emptyComments),
],
[],
Expand Down
2 changes: 1 addition & 1 deletion editor/src/sample-projects/sample-project-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ function createBeachesProjectContents(): ProjectContentTreeRoot {
lastParseSuccess: null,
lastSavedContents: null,
fileContents: {
code: "import * as React from 'react'\nimport { Scene, Storyboard } from 'utopia-api'\nimport { App } from '/src/app.js'\nimport { Playground } from '/src/playground.js'\n\nexport var storyboard = (\n <Storyboard>\n <Scene\n id='playground-scene'\n style={{\n width: 700,\n height: 759,\n position: 'absolute',\n left: 212,\n top: 128,\n }}\n data-label='Playground'\n >\n <Playground style={{}} />\n </Scene>\n <Scene\n id='app-scene'\n style={{\n width: 744,\n height: 1133,\n position: 'absolute',\n left: 1036,\n top: 128,\n }}\n data-label='My App'\n >\n <App style={{}} />\n </Scene>\n </Storyboard>\n)\n",
code: "import * as React from 'react'\nimport { Scene, Storyboard } from 'utopia-api'\nimport { App } from '/src/app.js'\nimport { Playground } from '/src/playground.js'\n\nexport var storyboard = (\n <Storyboard>\n <Scene\n id='playground-scene'\n data-comment-id='playground-scene'\n style={{\n width: 700,\n height: 759,\n position: 'absolute',\n left: 212,\n top: 128,\n }}\n data-label='Playground'\n >\n <Playground style={{}} />\n </Scene>\n <Scene\n id='app-scene'\n data-comment-id='app-scene'\n style={{\n width: 744,\n height: 1133,\n position: 'absolute',\n left: 1036,\n top: 128,\n }}\n data-label='My App'\n >\n <App style={{}} />\n </Scene>\n </Storyboard>\n)\n",
parsed: {
type: 'UNPARSED',
},
Expand Down
1 change: 1 addition & 0 deletions utopia-api/src/primitives/scene.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { View } from './view'
export interface SceneProps {
id?: string
style?: React.CSSProperties
'data-comment-id'?: string
'data-label'?: string
'data-uid'?: string
}
Expand Down
Loading