-
Notifications
You must be signed in to change notification settings - Fork 54
Google sheets plugin - image alt text support #673
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
35a6c2a
feat: add google sheets image alt text support
djohalo2 6944820
chore: refactor field type menu button into component
djohalo2 7ac6994
chore: extract sheets image id index function
djohalo2 281f019
chore: cleanup
djohalo2 262b3a9
fix: gracefully handle changing image field type with alt text pointer
djohalo2 d9a015b
chore: cleanup types
djohalo2 9558707
chore: rename imageColumnId
djohalo2 8e6a22c
chore: reuse resolved alt text pair logic
djohalo2 300e1e9
chore: filter out already assigned image columns from field type sele…
djohalo2 014cd60
chore: disable unavailable image fields instead of filtering them out
djohalo2 5142ced
chore: cleanup imageColumnId
djohalo2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
plugins/google-sheets/src/components/FieldTypeSelectField.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import cx from "classnames" | ||
| import { framer, type MenuItem } from "framer-plugin" | ||
| import React, { useMemo } from "react" | ||
| import { isAltTextColumn, type SheetCollectionFieldInput, type VirtualFieldType } from "../sheets" | ||
| import { IconChevron, IconChevronDown } from "./Icons" | ||
|
|
||
| interface FieldTypeOption { | ||
| type: VirtualFieldType | ||
| label: string | ||
| } | ||
|
|
||
| const fieldTypeOptions: FieldTypeOption[] = [ | ||
| { type: "string", label: "Plain Text" }, | ||
| { type: "formattedText", label: "Formatted Text" }, | ||
| { type: "date", label: "Date" }, | ||
| { type: "dateTime", label: "Date & Time" }, | ||
| { type: "link", label: "Link" }, | ||
| { type: "image", label: "Image" }, | ||
| { type: "color", label: "Color" }, | ||
| { type: "boolean", label: "Toggle" }, | ||
| { type: "number", label: "Number" }, | ||
| { type: "enum", label: "Option" }, | ||
| { type: "file", label: "File" }, | ||
| ] | ||
|
|
||
| const contextMenuOffset = 4 | ||
|
|
||
| interface Props { | ||
| field: SheetCollectionFieldInput | ||
| fields: SheetCollectionFieldInput[] | ||
| isDisabled: boolean | ||
| disabledFieldIds: Set<string> | ||
| onFieldTypeChange: (id: string, type: VirtualFieldType, imageColumnId?: string) => void | ||
| } | ||
|
|
||
| export function FieldTypeSelectField({ field, fields, isDisabled, disabledFieldIds, onFieldTypeChange }: Props) { | ||
| const imageColumn = fields.find(candidate => candidate.id === field.imageColumnId) | ||
|
|
||
| const fieldTypeLabel = useMemo(() => { | ||
| if (isAltTextColumn(field)) { | ||
| return imageColumn ? `Alt Text → ${imageColumn.name}` : "Alt Text" | ||
| } | ||
|
|
||
| return fieldTypeOptions.find(option => option.type === field.type)?.label ?? field.type | ||
| }, [field, imageColumn]) | ||
|
|
||
| const handleMenuOpen = (event: React.MouseEvent<HTMLButtonElement>) => { | ||
| const assignedImageColumnIds = new Set(fields.filter(isAltTextColumn).map(candidate => candidate.imageColumnId)) | ||
|
|
||
| const imageFields = fields.filter( | ||
| candidate => candidate.type === "image" && candidate.id !== field.id && !disabledFieldIds.has(candidate.id) | ||
| ) | ||
| const canAssignImageField = (candidate: SheetCollectionFieldInput) => | ||
| !assignedImageColumnIds.has(candidate.id) || candidate.id === field.imageColumnId | ||
|
|
||
| const items: MenuItem[] = [ | ||
| ...fieldTypeOptions.map(({ type, label }) => ({ | ||
| label, | ||
| checked: field.type === type, | ||
| onAction: () => { | ||
| onFieldTypeChange(field.id, type) | ||
| }, | ||
| })), | ||
| { | ||
| label: "Alt Text", | ||
| checked: isAltTextColumn(field), | ||
| enabled: imageFields.some(canAssignImageField), | ||
| submenu: imageFields.map(candidate => ({ | ||
| label: candidate.name, | ||
| checked: isAltTextColumn(field) && field.imageColumnId === candidate.id, | ||
| enabled: canAssignImageField(candidate), | ||
| onAction: () => { | ||
| onFieldTypeChange(field.id, "altText", candidate.id) | ||
| }, | ||
| })), | ||
| }, | ||
| ] | ||
|
|
||
| const { left, bottom, width } = event.currentTarget.getBoundingClientRect() | ||
| void framer.showContextMenu(items, { | ||
| location: { | ||
| x: left + width - contextMenuOffset, | ||
| y: bottom + contextMenuOffset, | ||
| }, | ||
| placement: "bottom-left", | ||
| width, | ||
| }) | ||
| } | ||
|
|
||
| return ( | ||
| <button | ||
| type="button" | ||
| className={cx("flex w-full min-w-0 items-center gap-1 text-left", isDisabled && "opacity-50")} | ||
| disabled={isDisabled} | ||
| title={fieldTypeLabel} | ||
| onClick={handleMenuOpen} | ||
| > | ||
| {isAltTextColumn(field) ? ( | ||
| <span className="flex min-w-0 flex-1 items-center gap-1"> | ||
| <span className="flex shrink-0 items-center gap-1"> | ||
| Alt Text | ||
| <IconChevron /> | ||
| </span> | ||
| <span className="truncate">{imageColumn?.name ?? "Image"}</span> | ||
| </span> | ||
| ) : ( | ||
| <span className="min-w-0 flex-1 truncate">{fieldTypeLabel}</span> | ||
| )} | ||
| <span className="shrink-0 text-content"> | ||
| <IconChevronDown /> | ||
| </span> | ||
| </button> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.