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 3 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
62 changes: 62 additions & 0 deletions examples-next/todo/keystone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,75 @@ const { withAuth } = createAuth({
},
});

export type ImageInTheDatabase = {
mode: 'local' | 'cloud';
// e.g 'c8cd1b99-8647-4b7e-a5d1-6693c79620a9'
id: string;
// source image extension
ext: string;
// blur hash
// blurhash: { hash: string; x: number; y: number };
// metadata about the source image
filesize: number;
width: number;
height: number;
};

/*
type UpdateImageInput {
upload: Upload
set: GraphqlImageInputType
delete: boolean;
}

mutation updatePost {
image: UpdateImageInput
}
*/

export type GraphqlImageInputType = {
field: {
mode: 'local' | 'cloud';
id: string;
Copy link
Contributor Author

@rohan-deshpande rohan-deshpande Mar 30, 2021

Choose a reason for hiding this comment

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

Probably only need mode and id for the input type, mode would be optional

width: number;
height: number;
// blurhash: { hash: string; x: number; y: number };
extension: string;
filesize: number;
};
};
export type GraphqlImageOutputType = {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

enum ImageObjectFit { FIXED INTRINSIC RESPONSIVE FILL }

ImageOutputType {
	id: ID!
	extension: TEXT!
	src: TEXT
	width: INTEGER
	height: INTEGER
	blurHash: { hash: TEXT, x: INTEGER, y: INTEGER }
	fileSize: INTEGER
	# Mabes add this later?
	transform (
		# This is "whatever the Next image component accepts"
		width: INTEGER
		height: INTEGER
		quality: INTEGER
		objectFit: ObjectFit
	) {
		src: TEXT
		width: INTEGER
		height: INTEGER
	}
}

field: {
mode: 'local' | 'cloud';
id: string;
// https://image.keystone.cloud/thinkmill/{id}.{ext}
// {keystone.config.publicPath}/{id}.{ext}
src: string;
width: number;
height: number;
// blurhash: { hash: string; x: number; y: number };
extension: string;
filesize: number;
};
};

export default withAuth(
config({
db: {
adapter: 'prisma_postgresql',
url: process.env.DATABASE_URL || 'postgres://keystone5:k3yst0n3@localhost:5432/todo-example',
},
lists,
images: {
/* could be 'cloud' */
upload: 'local',
local: {
/* where images get stored when uploaded */
uploadPath: './public/images',
/* the basePath for where image are served from */
publicPath: '/images',
},
},
ui: {
isAccessAllowed: ({ session }) => !!session,
},
Expand Down
3 changes: 2 additions & 1 deletion examples-next/todo/schema.ts
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
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