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

Snippets #55

Closed
wants to merge 35 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
955c6c4
init
hipstersmoothie Mar 27, 2019
cb180f9
required types
hipstersmoothie Mar 29, 2019
3b65d4f
default + type
hipstersmoothie Mar 29, 2019
ecac118
add some pizzaz
hipstersmoothie Mar 29, 2019
8d12c3f
lint
hipstersmoothie Mar 29, 2019
e7dc3d8
fix it after linting
hipstersmoothie Mar 29, 2019
d565727
Merge branch 'master' into descriptions
hipstersmoothie Mar 29, 2019
b2e275b
get rid of the copy pasta :tada:
hipstersmoothie Mar 29, 2019
c97b223
use lit-html for rendering the tooltip
hipstersmoothie Mar 29, 2019
34b1732
add example
hipstersmoothie Mar 29, 2019
5951248
fix lint + test
hipstersmoothie Mar 29, 2019
f5a4186
just use react!
hipstersmoothie Mar 29, 2019
d48b2e1
code improvements
hipstersmoothie Mar 29, 2019
9080d40
add snippets
hipstersmoothie Mar 30, 2019
1dc928a
only show tooltip when there is data. array is for possible prop values
hipstersmoothie Mar 31, 2019
194591f
ctrl+space will now toggle the hints (works with snippets and all sch…
hipstersmoothie Mar 31, 2019
b3bd132
Merge seek/master into snippets
hipstersmoothie Apr 25, 2019
bb8eeb2
don't display descriptions if there isn't one
hipstersmoothie Apr 26, 2019
2cbc8bb
handle long snippets
hipstersmoothie Apr 26, 2019
b46818a
render preview in preview area
hipstersmoothie May 6, 2019
0bb95e4
fix insertion
hipstersmoothie May 6, 2019
7c5d748
fix replacing typed characters
hipstersmoothie May 6, 2019
e06fe68
don't show completions in closing tag
hipstersmoothie May 6, 2019
b50d059
escape closes suggestions
hipstersmoothie May 6, 2019
1e31bbe
pressing escape also remove the preview from the rendered code
hipstersmoothie May 6, 2019
2b25a6c
fix focusing away from the snippet prompt
hipstersmoothie May 6, 2019
c0f4fdb
almost a combobox
hipstersmoothie May 6, 2019
18cc41f
dont' break the other hints
hipstersmoothie May 6, 2019
cbccc0d
run prettier on code snippet
hipstersmoothie May 6, 2019
2ee7d95
match codemirror cursor
hipstersmoothie May 6, 2019
bfd722f
get height to respect size
hipstersmoothie May 6, 2019
5a679ab
format just the snippets
hipstersmoothie May 6, 2019
a8771dd
fix flash + snippet list
hipstersmoothie May 6, 2019
0a0e46e
fix inserted snippet indent
hipstersmoothie May 6, 2019
664beee
only pad snippet if it's on it's own line
hipstersmoothie May 6, 2019
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ module.exports = {
Hello World!
</Button>
`,
// Press Ctrl+Space or Cmd+Space to choose from predefined snippets
snippets: {
Link: '<Link to="">Go Somewhere</Link>',
Button: `
<Button>
Hello World!
</Button>
`
},
webpackConfig: () => ({
// Custom webpack config goes here...
})
Expand Down
7 changes: 7 additions & 0 deletions cypress/integration/smokeTest.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,11 @@ describe('Smoke test', () => {
assertFrameContains('Foo');
assertTextareaContains('<Foo color="blue" />');
});

it('snippets', () => {
typeCode('{ctrl} ');
typeCode('{downarrow}{enter}');
assertFrameContains('Bar');
assertTextareaContains('<div>Bar</div>');
});
});
7 changes: 6 additions & 1 deletion cypress/projects/basic/playroom.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
module.exports = {
components: './components',
outputPath: './dist',
openBrowser: false
openBrowser: false,
snippets: {
Foo: '<div>Foo</div>',
Bar: '<div>Bar</div>',
Baz: '<div>Baz</div>'
}
};
7 changes: 7 additions & 0 deletions examples/typescript/components/Bar/Bar.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import React, { Component } from 'react';

interface Props {
/** The color of the Bar component's text */
color: 'red' | 'blue';
/**
* The count of schmeckles
* @default 42
*/
count?: number;
}

/** A fancy component */
export default class Bar extends Component<Props> {
render() {
const { color } = this.props;
Expand Down
12 changes: 11 additions & 1 deletion examples/typescript/components/Foo/Foo.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import React, { Component } from 'react';

interface Props {
color: 'red' | 'blue';
/**
* The color of the Foo components text
* @default 'red'
*/
color?: 'red' | 'blue';
/**
* Do something special
* @default false
*/
active?: boolean;
}

/** A basic component */
export default class Foo extends Component<Props> {
render() {
const { color } = this.props;
Expand Down
6 changes: 6 additions & 0 deletions examples/typescript/playroom.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
module.exports = {
components: './components/index.ts',
outputPath: './dist',

snippets: {
Foo: `<Foo color='red'>I am foo</Foo>`,
Bar: `<Bar color='blue'>I am Bar</Bar>`
},

webpackConfig: () => ({
module: {
rules: [
Expand Down
15 changes: 10 additions & 5 deletions lib/getStaticTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,16 @@ module.exports = async playroomConfig => {
const files = await fastGlob(typeScriptFiles, { cwd });
const types = parse(files);
const typesByDisplayName = keyBy(types, 'displayName');
const parsedTypes = mapValues(typesByDisplayName, component =>
mapValues(filterProps(component.props || {}), prop =>
parsePropTypeName(prop.type.name)
)
);
const parsedTypes = mapValues(typesByDisplayName, component => ({
component_description: component,
...mapValues(filterProps(component.props || {}), prop => ({
description: prop.description,
default: prop.defaultValue && prop.defaultValue.value,
required: prop.required,
type: prop.type.name.replace(/.\| undefined$/, ''),
values: parsePropTypeName(prop.type.name)
}))
}));

return parsedTypes;
} catch (err) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"mini-css-extract-plugin": "^0.4.3",
"opn": "^5.4.0",
"parse-prop-types": "^0.3.0",
"prettier": "^1.15.3",
"prettier": "^1.16.4",
"prop-types": "^15.6.2",
"query-string": "^6.1.0",
"re-resizable": "^4.9.3",
Expand Down
33 changes: 33 additions & 0 deletions src/Playroom/CodeMirror/extra-tooltip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from 'react';
import ReactDom from 'react-dom';
import styles from './extra-tooltip.less';

// Render a tooltip when getData returns something
const extraTooltip = (cm, hint, Component, getData) => {
const CodeMirror = cm.constructor;
const container = document.createElement('div');

CodeMirror.on(hint, 'close', () => container.remove());
CodeMirror.on(hint, 'update', () => container.remove());
CodeMirror.on(hint, 'select', (token, node) => {
const data = getData(token, node);
container.remove();

if (data) {
const x =
node.parentNode.getBoundingClientRect().right + window.pageXOffset;
const y = node.getBoundingClientRect().top + window.pageYOffset;

container.style.left = `${x}px`;
container.style.top = `${y}px`;
container.className = styles.container;

document.body.appendChild(container);
ReactDom.render(<Component data={data} cm={cm} />, container);
}
});

return hint;
};

export default extraTooltip;
14 changes: 14 additions & 0 deletions src/Playroom/CodeMirror/extra-tooltip.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.container {
position: absolute;
z-index: 10;
background: white;
border-radius: 3px;
font-family: monospace;
white-space: pre-wrap;
padding: 4px 8px;
box-shadow: 2px 3px 5px rgba(0, 0, 0, 0.2);
border: 1px solid silver;
max-width: 25em;
margin-left: 5px;
white-space: initial;
}
107 changes: 107 additions & 0 deletions src/Playroom/CodeMirror/prop-descriptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import React from 'react';
import styles from './prop-descriptions.less';
import extraTooltip from './extra-tooltip';

// Convert attribute values to arrays that addon-xml can handle
function prepareSchema(tags) {
return Object.keys(tags).reduce((all, key) => {
const tag = tags[key];

all[key] = {
...tag,
attrs: Object.keys(tag.attrs).reduce((allAttrs, name) => {
if (name === 'component_description') {
return allAttrs;
}

const attr = tag.attrs[name];
allAttrs[name] = Array.isArray(attr) ? attr : attr.values;
return allAttrs;
}, {})
};

return all;
}, {});
}

function getTypeColor(data) {
if (data.type === 'boolean') {
return 'rebeccapurple';
}

if (data.type === 'string' || data.values.length > 0) {
return 'darkred';
}

if (data.type === 'number') {
return 'steelblue';
}

return null;
}

const Tooltip = ({ data }) => (
<div>
{data.required && <span className={styles.required}>ⓘ</span>}
<span>{data.description}</span>
{data.default !== null && typeof data.default !== 'undefined' && (
<div className={styles.default}>
<span className={styles.defaultLabel}>Default:</span>
<span style={{ color: getTypeColor(data) }}>{data.default}</span>
</div>
)}
{data.type !== null && typeof data.type !== 'undefined' && (
<div className={styles.default}>
<span className={styles.defaultLabel}>Type:</span>
<span>{data.type}</span>
</div>
)}
</div>
);

function getAttribute(cm, tags, data) {
const CodeMirror = cm.constructor;
const cur = cm.getCursor();
const token = cm.getTokenAt(cur);

if (token.end > cur.ch) {
token.end = cur.ch;
token.string = token.string.slice(0, cur.ch - token.start);
}

const inner = CodeMirror.innerMode(cm.getMode(), token.state);
let attr;

// Attribute
if (tags[inner.state.tagName]) {
attr = tags[inner.state.tagName].attrs[data];
}

// Tag
if (data.match(/<\S+/)) {
attr = tags[data.slice(1)].attrs.component_description;
}

return attr;
}

export default function getHints(cm, options) {
const CodeMirror = cm.constructor;
const tags = options && options.schemaInfo;
const hint = CodeMirror.hint.xml(
cm,
Object.assign({}, options, {
schemaInfo: prepareSchema(tags)
})
);

return extraTooltip(cm, hint, Tooltip, token => {
const data = getAttribute(cm, tags, token);

if (Array.isArray(data) || (data && !data.description)) {
return null;
}

return data;
});
}
18 changes: 18 additions & 0 deletions src/Playroom/CodeMirror/prop-descriptions.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.required {
color: red;
padding-right: 5px;
}

.default {
margin-top: 5px;
}

.default:first-of-type {
margin-top: 10px;
}

.defaultLabel {
margin-right: 8px;
font-weight: 500;
color: rgba(0, 0, 0, 0.7);
}
Loading