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

[WIP] Next image service #5272

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ export default withAuth(
url: process.env.DATABASE_URL || 'postgres://keystone5:k3yst0n3@localhost:5432/todo-example',
},
lists,
images: {
/* could be 'cloud' */
upload: 'local',
JedWatson marked this conversation as resolved.
Show resolved Hide resolved
local: {
/* where images get stored when uploaded */
uploadPath: './public/images',
JedWatson marked this conversation as resolved.
Show resolved Hide resolved
/* the basePath for where image are served from */
publicPath: '/images',
},
},
ui: {
isAccessAllowed: ({ session }) => !!session,
},
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createSchema, list } from '@keystone-next/keystone/schema';
import { checkbox, password, relationship, text, timestamp } from '@keystone-next/fields';
import { checkbox, image, password, relationship, text, timestamp } from '@keystone-next/fields';

// this implementation for createdBy and updatedBy is currently wrong so they're disabled for now
const trackingFields = {
Expand Down Expand Up @@ -69,6 +69,7 @@ export const lists = createSchema({
name: text({ isRequired: true }),
email: text(),
password: password(),
profilePicture: image(),
tasks: relationship({
ref: 'Todo.assignedTo',
many: true,
Expand Down
34 changes: 34 additions & 0 deletions examples-next/images/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
type ImageMode = 'local' | 'cloud';
JedWatson marked this conversation as resolved.
Show resolved Hide resolved
type ImageFormat = 'jpeg' | 'jpg' | 'png' | 'gif' | 'webp';
type ImageObjectFit = 'fixed' | 'intrinsic' | 'responsive' | 'fill';

export type Image = {
mode: ImageMode;
id: string;
extension: ImageFormat;
filesize: number;
width: number;
height: number;
};

export type ImageInputType = {
upload: object;
id: string;
mode: ImageMode;
};

export type ImageOutputType = {
mode: ImageMode;
id: string;
ext: string;
src: string;
width: number;
height: number;
filesize: number;
transform: (
width: number,
height: number,
quality: number,
objectFit: ImageObjectFit
) => { src: string; width: number; height: number };
};
1 change: 1 addition & 0 deletions packages-next/fields/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export { relationship } from './types/relationship';
export { text } from './types/text';
export { password } from './types/password';
export { timestamp } from './types/timestamp';
export { image } from './types/image';
export { integer } from './types/integer';
export { float } from './types/float';
export { decimal } from './types/decimal';
Expand Down
23 changes: 23 additions & 0 deletions packages-next/fields/src/types/image/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// @ts-ignore
import { Text } from '@keystone-next/fields-legacy';
import type { FieldType, BaseGeneratedListTypes } from '@keystone-next/types';
import { resolveView } from '../../resolve-view';
import type { FieldConfig } from '../../interfaces';

export type ImageFieldConfig<
TGeneratedListTypes extends BaseGeneratedListTypes
> = FieldConfig<TGeneratedListTypes> & {
isRequired?: boolean;
isIndexed?: boolean;
};

export const image = <TGeneratedListTypes extends BaseGeneratedListTypes>(
config: ImageFieldConfig<TGeneratedListTypes> = {}
): FieldType<TGeneratedListTypes> => ({
type: Text,
config,
views: resolveView('image/views'),
getAdminMeta: () => ({
/* ?? */
}),
});
43 changes: 43 additions & 0 deletions packages-next/fields/src/types/image/tests/test-fixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { AdapterName } from '@keystone-next/test-utils-legacy';
import { text } from '..';

export const name = 'Text';
export const typeFunction = text;
export const exampleValue = () => 'foo';
export const exampleValue2 = () => 'bar';
export const supportsUnique = true;
export const fieldName = 'testField';

export const getTestFields = () => ({ name: text(), testField: text() });

export const initItems = () => {
return [
{ name: 'a', testField: '' },
{ name: 'b', testField: 'other' },
{ name: 'c', testField: 'FOOBAR' },
{ name: 'd', testField: 'fooBAR' },
{ name: 'e', testField: 'foobar' },
{ name: 'f', testField: null },
{ name: 'g' },
];
};

export const storedValues = () => [
{ name: 'a', testField: '' },
{ name: 'b', testField: 'other' },
{ name: 'c', testField: 'FOOBAR' },
{ name: 'd', testField: 'fooBAR' },
{ name: 'e', testField: 'foobar' },
{ name: 'f', testField: null },
{ name: 'g', testField: null },
];

export const supportedFilters = (adapterName: AdapterName) => [
'null_equality',
'equality',
adapterName !== 'prisma_sqlite' && 'equality_case_insensitive',
'in_empty_null',
'in_value',
adapterName !== 'prisma_sqlite' && 'string',
adapterName !== 'prisma_sqlite' && 'string_case_insensitive',
];
58 changes: 58 additions & 0 deletions packages-next/fields/src/types/image/views/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* @jsx jsx */

import { CellContainer, CellLink } from '@keystone-next/admin-ui/components';
import {
CardValueComponent,
CellComponent,
FieldController,
FieldControllerConfig,
FieldProps,
} from '@keystone-next/types';
import { jsx } from '@keystone-ui/core';
import { FieldContainer, FieldLabel, TextInput } from '@keystone-ui/fields';

export const Field = ({ field, value, onChange, autoFocus }: FieldProps<typeof controller>) => (
<FieldContainer>
<FieldLabel>{field.label}</FieldLabel>
{onChange ? (
<TextInput
autoFocus={autoFocus}
onChange={event => onChange(event.target.value)}
value={value}
/>
) : (
value
)}
</FieldContainer>
);

export const Cell: CellComponent = ({ item, field, linkTo }) => {
let value = item[field.path] + '';
return linkTo ? <CellLink {...linkTo}>{value}</CellLink> : <CellContainer>{value}</CellContainer>;
};
Cell.supportsLinkTo = true;

export const CardValue: CardValueComponent = ({ item, field }) => {
return (
<FieldContainer>
<FieldLabel>{field.label}</FieldLabel>
{item[field.path]}
</FieldContainer>
);
};

type Config = FieldControllerConfig;

export const controller = (config: Config): FieldController<string, string> => {
return {
path: config.path,
label: config.label,
graphqlSelection: config.path,
defaultValue: '',
deserialize: data => {
const value = data[config.path];
return typeof value === 'string' ? value : '';
},
serialize: value => ({ [config.path]: value }),
};
};
19 changes: 19 additions & 0 deletions packages-next/types/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ import type { ListAccessControl, FieldAccessControl } from './access-control';
import type { ListHooks } from './hooks';

export type KeystoneConfig = {
// e.g 'thinkmill:243809dsjkfdls-r2y8osdfjsdf-23y8osf',
cloud?: string;
JedWatson marked this conversation as resolved.
Show resolved Hide resolved
lists: ListSchemaConfig;
db: DatabaseConfig;
ui?: AdminUIConfig;
images?: ImagesConfig;
server?: ServerConfig;
session?: () => SessionStrategy<any>;
graphql?: GraphQLConfig;
Expand Down Expand Up @@ -106,6 +109,22 @@ export type AdminFileToWrite =
| { mode: 'write'; src: string; outputPath: string }
| { mode: 'copy'; inputPath: string; outputPath: string };

// config.images

export type ImagesConfig =
| {
upload: 'local';
local: {
/** The path local images are uploaded to */
uploadPath?: string;
/** The path local images will be served from, outside of keystone */
publicPath?: string;
};
}
| {
upload: 'cloud';
};

// config.server

export type ServerConfig = {
Expand Down