Skip to content

Commit

Permalink
2.0.5 (#66)
Browse files Browse the repository at this point in the history
* # 2.0.3
@craftercms/classes
- Change SDKService.httpGet to use fetch instead of rxjs.ajax
- Fix fetch's mode (for cors)
- Extend support for other fetch modes
- Switch from using URLSearchParams to using query-string for the same purpose

* # 2.0.4
@craftercms/content
- Fix `flatten` argument to getDescriptor getting lost/ignored
@craftercms/models
- Move types from other files into models package
- Adding missing properties to NavigationItem interface

* # 2.0.5
@craftercms/content
- Add prop data type parsing to `parsedDescriptor`
@craftercms/ice
- Add v4 support to `addAuthoringSupport`

* ## 2.0.5

### @craftercms/content
- Add prop data type parsing to `parsedDescriptor`

### @craftercms/ice
- Add v4 support to `addAuthoringSupport`
  • Loading branch information
rart authored Sep 2, 2022
1 parent d5c853a commit 223bb59
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 17 deletions.
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
# SDK Changelog

## 2.0.5

### @craftercms/content
- Add prop data type parsing to `parsedDescriptor`

### @craftercms/ice
- Add v4 support to `addAuthoringSupport`

## 2.0.4

### @craftercms/content
- Fix `flatten` argument to getDescriptor getting lost/ignored

### @craftercms/models
- Move types from other files into models package
- Adding missing properties to NavigationItem interface

## 2.0.3

### @craftercms/classes
- Change SDKService.httpGet to use fetch instead of rxjs.ajax
- Fix fetch's mode (for cors)
- Extend support for other fetch modes
- Switch from using URLSearchParams to using query-string for the same purpose

## 2.0.2
- Allow CORs
- Use `crafterConf.configure({ cors: true, ... })` to enable CORs mode.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@craftercms/sdk",
"version": "2.0.4",
"version": "2.0.5",
"private": true,
"workspaces": [
"packages/*"
Expand Down
50 changes: 40 additions & 10 deletions packages/content/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,18 @@ const systemProps = Object.keys(systemPropMap).concat(
Object.values(systemPropMap)
);

export function parseDescriptor(data: DescriptorResponse | DescriptorResponse[]): ContentInstance | ContentInstance[] {
export interface ParseDescriptorOptions {
/** Whether to parse the field values to their respective data type based on postfix (e.g. _i number, _b boolean, etc) */
parseFieldValueTypes: boolean;
}

export function parseDescriptor(data: DescriptorResponse | DescriptorResponse[], options?: ParseDescriptorOptions): ContentInstance | ContentInstance[] {
if (data == null) {
return null;
} else if (Array.isArray(data)) {
return data.map((item) => parseDescriptor(item) as ContentInstance);
return data.map((item) => parseDescriptor(item, options) as ContentInstance);
} else if (data.children) {
return parseDescriptor(extractChildren(data.children));
return parseDescriptor(extractChildren(data.children), options);
} else if (data.descriptorDom === null && data.descriptorUrl) {
// This path catches calls to getChildren (/api/1/site/content_store/children.json?url=&crafterSite=)
// The getChildren call contains certain items that can't be parsed into content items.
Expand All @@ -77,10 +82,10 @@ export function parseDescriptor(data: DescriptorResponse | DescriptorResponse[])
sourceMap: {}
}
};
return parseProps(extractContent(data), parsed);
return parseProps(extractContent(data), parsed, options);
}

export function parseProps<Props = object, Target = object>(props: Props, parsed: Target = {} as Target): Target {
export function parseProps<Props = object, Target = object>(props: Props, parsed: Target = {} as Target, options: ParseDescriptorOptions = { parseFieldValueTypes: false }): Target {
Object.entries(props).forEach(([prop, value]) => {
if (ignoreProps.includes(prop)) {
return; // continue, skip prop.
Expand Down Expand Up @@ -113,7 +118,7 @@ export function parseProps<Props = object, Target = object>(props: Props, parsed
}
parsed[prop] = parsed[prop].map((item) => {
const { key, value, component, include } = item;
if ((item.component) || (item.key && item.value)) {
if ((item.component) || (item.key && item.include)) {
// Components
const newComponent = {
label: value,
Expand All @@ -126,19 +131,44 @@ export function parseProps<Props = object, Target = object>(props: Props, parsed
: component?.path ? component.path : null
)
};
return parseDescriptor(newComponent);
return parseDescriptor(newComponent, options);
} else {
// Repeat group items
return parseProps(item);
// Repeat group items or key/value pairs
return parseProps(item, void 0, options);
}
});
} else {
parsed[prop] = value ?? null;
parsed[prop] = value != null
? options.parseFieldValueTypes ? parseFieldValue(prop, value) : value
: null;
}
});
return parsed;
}

export function parseFieldValue(propName: string, propValue: any): number | string | boolean {
let postFix = propName.substr(propName.lastIndexOf('_'));
switch (postFix) {
/*
_i For integer number.
_l For long integer number.
_f For floating point number.
_d For long floating point number.
_to For time
_dt For date in ISO 8601
*/
case '_b':
return propValue.toLowerCase().trim() === 'true';
case '_i':
case '_l':
case '_f':
case '_d':
return parseFloat(propValue);
default:
return propValue;
}
}

/**
* Inspects the data for getItem or getDescriptor responses and returns the inner content object
* */
Expand Down
24 changes: 18 additions & 6 deletions packages/ice/ice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import { crafterConf } from '@craftercms/classes';

declare namespace window {
const crafterRequire: any;
const craftercms: {
xb: {
initInContextEditing(props: { path: string; props: Record<string, any> }): { unmount(): void }
}
};
}

export interface BaseCrafterConfig extends Pick<CrafterConfig, 'site' | 'baseUrl'> {
Expand Down Expand Up @@ -60,17 +65,24 @@ const printedErrorCache = {
invalidPath: {}
};

export function addAuthoringSupport(config?: Partial<BaseCrafterConfig>): Promise<any> {
export function addAuthoringSupport(config?: Partial<BaseCrafterConfig & { xb?: boolean }>): Promise<any> {
const isV4 = Boolean(config?.xb);
config = crafterConf.mix(config);
return new Promise((resolve) => {
const script = document.createElement('script');
script.src = `${config.baseUrl}/studio/static-assets/libs/requirejs/require.js`;
script.src = isV4
? `${config.baseUrl}/studio/static-assets/scripts/craftercms-xb.umd.js`
: `${config.baseUrl}/studio/static-assets/libs/requirejs/require.js`;
script.addEventListener('load', () => {
window.crafterRequire?.([`${config.baseUrl}/studio/overlayhook?.js`], () => {
window.crafterRequire(['guest'], (guest) => {
resolve(guest);
if (isV4) {
resolve(window.craftercms?.xb);
} else {
window.crafterRequire?.([`${config.baseUrl}/studio/overlayhook?.js`], () => {
window.crafterRequire(['guest'], (guest) => {
resolve(guest);
});
});
});
}
});
document.head.appendChild(script);
});
Expand Down

0 comments on commit 223bb59

Please sign in to comment.