import "./index.scss";
import React = require("react");
import cx = require("classnames");
import { CSSUnitInputComponent } from "./common";
import { BaseApplicationComponent } from "@tandem/common";
import { MetadataKeys } from "@tandem/editor/browser/constants";
import { RadioGroupComponent, TextInputComponent } from "@tandem/uikit";
import { ChromePicker } from "react-color";
import AutoSizeInput = require("react-input-autosize");
import ReactSliderComponent = require("react-slider");
import { CSSMergedRuleLinkComponent, CSSHighlightTargetRuleHintComponent } from "../common";
import Select = require("react-select");
import CheckboxComponent = require("rc-checkbox");
import { capitalize, startCase } from "lodash";
import { SUPPORTED_FONTS } from "@tandem/html-extension/editor/browser/constants";
import {
parseCSSDeclValue,
SyntheticCSSColor,
SyntheticCSSStyle,
SyntheticCSSFilter,
SyntheticAmountFilter,
SyntheticDropShadowFilter,
getCSSFontFaceRules,
SyntheticCSSFontFace,
evaluateCSSDeclValue,
SyntheticHTMLElement,
SyntheticCSSStyleGraphics,
SyntheticCSSStyleBoxShadow,
SyntheticCSSStyleBackground,
SyntheticCSSElementStyleRule,
} from "@tandem/synthetic-browser";
import { MergedCSSStyleRule } from "@tandem/html-extension/editor/browser/stores";
// http://www.w3schools.com/csSref/pr_class_display.asp
const DISPLAY_OPTIONS = ["block", "inline", "inline-block", "flex", "none", "table"].map((value) => {
return { label: value, value: value };
});
// https://css-tricks.com/snippets/css/a-guide-to-flexbox/
const FLEX_DIRECTION_OPTIONS = ["row", "column"].map((value) => {
return { label: value, value: value };
});
const FLEX_WRAP_OPTIONS = ["nowrap", "wrap", "wrap-reverse"].map((value) => {
return { label: value, value: value };
});
const FLEX_FLOW_OPTIONS = ["start", "end", "center", "space-bwtween", "space-around"].map((value) => {
return { label: value, value: value };
});
const FLEX_JUSTIFY_CONTENT_OPTIONS = ["flex-start", "flex-end", "center", "space-between", "space-around"].map((value) => {
return { label: value, value: value };
});
const FLEX_ALIGN_ITEMS_OPTIONS = ["flex-start", "flex-end", "center", "baseline"].map((value) => {
return { label: value, value: value };
});
const FLEX_ALIGN_CONTENT_OPTIONS = ["flex-start", "flex-end", "center", "space-between", "space-around", "stretch"].map((value) => {
return { label: value, value: value };
});
// http://www.w3schools.com/css/css_positioning.asp
const POSITION_OPTIONS = ["static", "relative", "fixed", "absolute"].map((value) => {
return { label: value, value: value };
});
// http://www.w3schools.com/cssref/pr_background-blend-mode.asp
const BLEND_MODE_OPTIONS = ["normal", "multiply", "screen", "overlay", "darken", "lighten", "color-dodge", "saturation", "color", "lumocity"].map((value) => {
return { label: value, value: value };
});
// http://www.w3schools.com/cssref/pr_background-blend-mode.asp
const TEXT_ALIGN_OPTIONS = ["left", "center", "right", "justify"].map((value) => {
return { label: value, value: value };
});
// http://www.w3schools.com/cssref/pr_background-blend-mode.asp
const TEXT_DECOR_OPTIONS = ["none", "underline", "overline", "line-through"].map((value) => {
return { label: value, value: value };
});
const DEFAULT_FONT_FAMILY_OPTIONS = SUPPORTED_FONTS.map((value) => {
return { label: value, value: value };
});
const BACKGROUND_REPEAT_OPTIONS = ["repeat", "repeat-x", "repeat-y", "no-repeat"].map((value) => {
return { label: value, value: value };
});
const WORD_WRAP_OPTIONS = ["normal", "break-word", "initial"].map((value) => {
return { label: value, value: value };
});
const TEXT_OVERFLOW_OPTIONS = ["initial", "ellipsis", "clip"].map((value) => {
return { label: value, value: value };
});
const WHITE_SPACE_OPTIONS = ["normal", "nowrap", "pre", "pre-line", "pre-wrap"].map((value) => {
return { label: value, value: value };
});
const LAYOUT_OVERFLOW_OPTIONS = ["visible", "hidden", "scroll"].map((value) => {
return { label: value, value: value };
});
const FLOAT_OPTIONS = ["none", "left", "right", "inline-start", "inline-end"].map((value) => {
return { label: value, value: value };
});
const FONT_STYLE_OPTIONS = ["normal", "italic", "oblique", "initial"].map((value) => {
return { label: value, value: value };
});
const CSS_FILTER_OPTIONS = ["blur", "brightness", "contrast", "drop-shadow", "grayscale", "hue-rotate", "invert", "opacity", "saturate", "sepia"].map((value) => {
return { label: value, value: value };
});
const TEXT_TRANSFORM_OPTIONS = ["none", "uppercase", "lowercase", "capitalize"].map((value) => {
return { label: value, value: value };
});
const DISPLAY_PREVIEWS = {
block: () => {
return
},
inline: () => {
return
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In velit.
},
"inline-block": () => {
return
},
"flex": () => {
return
}
}
const FLEX_ALIGN_ITEMS_PREVIEWS = {
}
const FLEX_ALIGN_CONTENT_PREVIEWS = {
}
const FLEX_DIRECTION_PREVIEWS = {
row: () => {
return
},
column: () => {
return
}
}
const FLEX_WRAP_PREVIEWS = {
}
const createPreviewMenuOption = (previewRenderers: any) => {
return (option) => {
return { option.label }
}
}
export class CSSPrettyInspectorComponent extends BaseApplicationComponent<{ rule: MergedCSSStyleRule, graphics: SyntheticCSSStyleGraphics }, any> {
render() {
const { rule } = this.props;
const graphics = rule.graphics;
//
//
return
{ this.renderAppearance() }
{ this.renderAnimations() }
}
renderAppearance() {
const { rule, graphics } = this.props;
return ;
}
renderAnimations() {
const { graphics } = this.props;
// unblock when actually working
if (1 + 1) return null;
// TODO - more buttons needs to popup an animation timeline footer
return ;
}
}
function bindGraphicInputEvent(graphics: SyntheticCSSStyleGraphics|SyntheticCSSStyleBoxShadow|SyntheticCSSStyleBackground, propertyName: string) {
return (event: React.KeyboardEvent) => {
graphics.setProperty(propertyName, event.currentTarget.value);
}
}
function bindGraphicSelectChange(graphics: SyntheticCSSStyleGraphics|SyntheticCSSStyleBoxShadow|SyntheticCSSStyleBackground, propertyName: string, map?: (value) => any) {
if (!map) map = value => value;
return (option) => {
graphics.setProperty(propertyName, option ? map(option.value) : undefined);
}
}
function bindMergedRuleSelectChange(rule: MergedCSSStyleRule, propertyName?: string) {
return (option) => {
rule.setSelectedStyleProperty(propertyName, option.value);
}
}
function bindMergedRuleValueChange(rule: MergedCSSStyleRule, propertyName?: string) {
return (value) => {
rule.setSelectedStyleProperty(propertyName, value);
}
}
function bindGraphicsValueChange(graphics: SyntheticCSSStyleGraphics|SyntheticCSSStyleBoxShadow|SyntheticCSSStyleBackground|SyntheticCSSFilter, propertyName: string) {
return (value) => {
graphics[propertyName] = value;
}
}
type OpenPopupFunction = (title: string, renderBody: () => any) => any;
interface ISectionComponentProps {
rule: MergedCSSStyleRule;
graphics: SyntheticCSSStyleGraphics;
}
interface ISectionComponentPopup {
title: string;
renderBody: () => any;
}
abstract class SectionComponent extends React.Component {
protected _popup: ISectionComponentPopup;
openPopup = (title: string, renderBody: () => any) => {
this._popup = { title, renderBody };
this.setState({});
}
closePopup = () => {
this._popup = undefined;
this.onClosePopup();
this.setState({});
}
onClosePopup() {
}
render() {
const mainSection = this.renderMainSection();
if (!mainSection) return null;
return
{this._popup ? this.renderPopup() : undefined }
{mainSection}
;
}
renderPopup() {
return this.closePopup()} />;
}
abstract renderMainSection();
}
class SidebarPopupComponent extends React.Component {
state = {
stickToBottom: false
}
componentDidMount() {
const popup = (this.refs as any).popup;
const rect = popup.getBoundingClientRect();
if (rect.bottom > window.innerHeight) {
this.setState({ stickToBottom: true });
}
document.body.addEventListener("click", this.onDocumentClick);
}
onDocumentClick = (event: MouseEvent) => {
const popup = (this.refs as any).popup as HTMLElement;
if (event.target === popup || popup.contains(event.target as HTMLElement)) {
return;
}
this.props.closePopup();
}
componentWillUnmount() {
document.body.removeEventListener("click", this.onDocumentClick);
}
render() {
return
{ this.props.title }
this.props.closePopup()} />
{this.props.renderBody && this.props.renderBody() }
;
}
}
class FilterSectionComponent extends SectionComponent {
private _selectedFilterIndex: number = -1;
selectFilter = (filter: SyntheticCSSFilter) => {
this._selectedFilterIndex = this.props.graphics.filters.indexOf(filter);
if (filter) {
this.openPopup(startCase(filter.name), this.renderFilterOptions);
} else {
this.closePopup();
}
}
onClosePopup() {
this._selectedFilterIndex = -1;
}
componentDidMount() {
// if (process.env.SANDBOXED) {
// this.selectFilter(this.props.graphics.filters[2]);
// }
}
renderMainSection() {
const { graphics, rule } = this.props;
const selectedFilterIndex = this._selectedFilterIndex;
return
Filters
{
this.selectFilter(undefined);
graphics.removeFilter(graphics.filters[selectedFilterIndex]);
}} />
{
graphics.addFilter("drop-shadow", [0, 0, 3, "#000"]);
}} />
{
graphics.filters.map((filter, i) => {
return
{
graphics.renameFilter(filter, newName);
}} />;
})
}
}
renderFilterOptions = () => {
const filter = this.props.graphics.filters[this._selectedFilterIndex];
if (!filter) return null;
const renderPercentFilter = (filter: SyntheticAmountFilter, max: number = 100) => {
const setNewAmount = (newValue) => {
filter.setProperty("amount", newValue + "%");
}
return
}
const renderLengthFilter = (filter: SyntheticAmountFilter) => {
const setNewAmount = (newValue) => {
filter.setProperty("amount", newValue + "px");
}
return
}
const renderDropShadowFilter = (filter: SyntheticDropShadowFilter) => {
return
}
const renderAngleFilter = (filter: SyntheticAmountFilter) => {
const setNewAmount = (newValue) => {
filter.setProperty("amount", newValue + "deg");
}
return
}
// https://developer.mozilla.org/en-US/docs/Web/CSS/filter
const renderer = {
"blur": renderLengthFilter,
"brightness": filter => renderPercentFilter(filter, 1000),
"contrast": filter => renderPercentFilter(filter, 1000),
"drop-shadow": renderDropShadowFilter,
"grayscale": renderPercentFilter,
"hue-rotate": renderAngleFilter,
"invert": renderPercentFilter,
"opacity": renderPercentFilter,
"sepia": renderPercentFilter,
"saturate": filter => renderPercentFilter(filter, 1000),
}[filter.name];
return
{ renderer && renderer(filter) }
}
}
class StylePropertyPreviewComponent extends React.Component<{ renderer(): any }, any> {
render() {
const { renderer } = this.props;
return
{ renderer && renderer() }
{ this.props.children }
}
}
class LayoutSectionComponent extends SectionComponent {
componentDidMount() {
// if (process.env.SANDBOXED) {
// this.openPopup("Advanced", this.renderAdvancedSection);
// }
}
renderMainSection() {
const { rule, graphics } = this.props;
return
Layout
{
this.openPopup("Advanced", this.renderAdvancedSection);
}} />
}
renderAdvancedSection = () => {
const { graphics, rule } = this.props;
return
}
}
class FlexContainerSectionComponent extends SectionComponent {
componentDidMount() {
if (process.env.SANDBOXED) {
this.openPopup("Advanced", this.renderAdvancedSection);
}
}
renderMainSection() {
const { graphics, rule } = this.props;
if (graphics.display !== "flex") return null;
return
Flex Container
{
this.openPopup("Advanced", this.renderAdvancedSection);
}} />
}
renderAdvancedSection = () => {
const { rule } = this.props;
return
}
}
class FlexContainerChildSectionComponent extends SectionComponent {
componentDidMount() {
// if (process.env.SANDBOXED) {
// this.openPopup("Advanced", this.renderAdvancedSection);
// }
}
renderMainSection() {
// OTHERS - align-self, flex, flex-basis
const { graphics, rule } = this.props;
// yuck - checking computed property here just to get it to work
if (!rule.target.parentElement || ((rule.target.parentElement as SyntheticHTMLElement).getComputedStyle() || {} as any).display !== "flex") return null;
return ;
// if (!rule.target.parentNode || rule.target.parentNode) return null;
}
}
class TypographySectionComponent extends SectionComponent {
componentDidMount() {
// if (process.env.SANDBOXED) {
// this.openPopup("Advanced", this.renderAdvancedSection);
// }
}
// TODO - implement this
previewFont = (option) => {
// if (option == null) {
// this.props.rule.getTargetRule("fontFamily").style.setProperty("fontFamily", this.props.rule.style.fontFamily);
// } else {
// console.log(this.props.rule.getTargetRule("fontFamily").style);
// this.props.rule.getTargetRule("fontFamily").style.setProperty("fontFamily", option.value);
// }
}
renderMainSection() {
const { rule, graphics } = this.props;
// TODO - preview font on key down
const renderFontOption = (option) => {
return { option.label }
};
return
Typography
{
this.openPopup("Advanced", this.renderAdvancedSection);
}} />
Font
[`'${font}'`])} valueRenderer={renderFontOption} onFocus={this.previewFont} optionRenderer={renderFontOption} />
Size
Color
this.openPopup("Font Color", this.renderFontColorPicker)
} />
}
renderAdvancedSection = () => {
const { graphics, rule } = this.props;
return
}
getFontFamilyOptions = () => {
return [...getCSSFontFaceRules(this.props.rule.target).map((rule) => {
const font = String(rule.style.fontFamily).replace(/["']/g, "");
return { value: font, label: font };
}), ...DEFAULT_FONT_FAMILY_OPTIONS];
}
renderFontColorPicker = () => {
const { rule, graphics } = this.props;
return
graphics.color = new SyntheticCSSColor(color.rgb.r, color.rgb.g, color.rgb.b, color.rgb.a)} />
}
}
class BackgroundsSectionComponent extends SectionComponent {
private _selectedBackgroundIndex: number = -1;
selectBackground = (background: SyntheticCSSStyleBackground) => {
this._selectedBackgroundIndex = this.props.graphics.backgrounds.indexOf(background);
if (background == null) {
this.closePopup();
} else {
this.openPopup("Options", this.renderFill);
}
}
onClosePopup() {
this._selectedBackgroundIndex = -1;
}
renderMainSection() {
const { rule, graphics } = this.props;
const selectedBackgroundIndex = this._selectedBackgroundIndex;
const labelClassnames = cx({ row: true, labels: true, hide: graphics.backgrounds.length === 0 });
return
Backgrounds
{
this.selectBackground(undefined);
graphics.removeBackground(graphics.backgrounds[selectedBackgroundIndex]);
}} />
{
this.selectBackground(graphics.addBackground([new SyntheticCSSColor(0, 0, 0, 1)]));
}} />
{
graphics.backgrounds.map((background, i) => {
return
})
}
;
}
renderFill = () => {
const background = this.props.graphics.backgrounds[this._selectedBackgroundIndex];
if (!background) return null;
return
{
background.color = SyntheticCSSColor.fromRGBA(rgb);
}} />
}
}
class BoxShadowsSectionComponent extends SectionComponent {
private _selectedBoxShadowIndex: number = -1;
componentDidMount() {
// if (process.env.SANDBOXED) {
// this._selectedBoxShadowIndex = 0;
// this.openPopup("Options", this.renderShadowOptions);
// }
}
selectBoxShadow = (boxShadow: SyntheticCSSStyleBoxShadow) => {
this._selectedBoxShadowIndex = this.props.graphics.boxShadows.indexOf(boxShadow);
if (this._selectedBoxShadowIndex === -1) {
this.closePopup();
} else {
this.openPopup("Options", this.renderShadowOptions);
}
}
onClosePopup() {
this._selectedBoxShadowIndex = -1;
}
renderMainSection() {
const { graphics, rule } = this.props;
const selectedBoxShadowIndex = this._selectedBoxShadowIndex;
const labelClassnames = cx({ row: true, labels: true, hide: graphics.boxShadows.length === 0 });
return
Box shadows
{
this.selectBoxShadow(undefined);
graphics.removeBoxShadow(graphics.boxShadows[selectedBoxShadowIndex]);
}} />
{
this.selectBoxShadow(graphics.addBoxShadow([0, 0, 2, 1, new SyntheticCSSColor(0, 0, 0, 1)]));
}} />
{
graphics.boxShadows.map((background, i) => {
return
})
}
;
}
renderShadowOptions = () => {
const boxShadow = this.props.graphics.boxShadows[this._selectedBoxShadowIndex] || new SyntheticCSSStyleBoxShadow([0, 0, 0, 0, new SyntheticCSSColor(0, 0, 0, 1)])
if (!boxShadow) return null;
return ;
}
}
class CSSShadowInputComponent extends React.Component<{ target: SyntheticCSSStyleBoxShadow|SyntheticDropShadowFilter }, any> {
render() {
const { target } = this.props;
return
{ !process.env.HIDE_COLOR_PICKER ? {
target.color = SyntheticCSSColor.fromRGBA(rgb);
}} /> : null }
;
}
}
class CSSBackgroundInputComponent extends React.Component<{ background: SyntheticCSSStyleBackground, rule: MergedCSSStyleRule, select: (background: SyntheticCSSStyleBackground) => any }, any> {
componentDidMount() {
// this.props.openPopup("Fill", this.renderFill);
}
render() {
const { background, select, rule } = this.props;
const { color, blendMode, clip } = background;
return
{
select(background);
}} />
;
}
}
class CSSBoxSectionComponent extends SectionComponent {
private _selectedBoxName: string;
selectBox(name: string) {
this._selectedBoxName = name;
if (name) {
this.openPopup(name, this.renderBoxInputs);
} else {
this.closePopup();
}
}
componentDidMount() {
if (process.env.SANDBOXED) {
// this.selectBox("padding");
}
}
onClosePopup() {
this._selectedBoxName = undefined;
}
renderMainSection() {
const selectedBox = this._selectedBoxName;
return
{
this.renderBox(
"margin",
this.renderBox(
"border",
this.renderBox(
"padding",
this.renderBox(
"content"
)
)
)
)
}
}
renderBoxInputs = () => {
if (!this._selectedBoxName) return null;
if (this._selectedBoxName === "border") return this.renderBorderInputs();
return this.renderSpacedInput();
}
renderBorderInputs() {
return
BORDER
}
renderSpacedInput() {
const box = {} as any;
return
}
renderBox(name: string, child?: any) {
const classNames = cx({ "focused": name === this._selectedBoxName, box: true, [name]: true, "not-nested": true });
return {
event.stopPropagation();
this.selectBox(name);
}}>
{ name.substr(0, 1).toUpperCase() + name.substr(1).toLowerCase() }
10
10
10
10
{ child }
}
}
class CSSBoxShadowInputComponent extends React.Component<{ boxShadow: SyntheticCSSStyleBoxShadow, rule: MergedCSSStyleRule, select: (shadow: SyntheticCSSStyleBoxShadow) => any }, any> {
render() {
const { boxShadow, rule } = this.props;
const { color, x, y, blur, spread, inset } = boxShadow;
return
{
this.props.select(boxShadow);
}} />
}
}
export class BetterCheckbox extends React.Component<{ onChange(newValue): any, checked: any }, { currentValue }> {
state = {
currentValue: undefined
}
onChange = (event: any) => {
this.props.onChange(!!event.target.checked);
}
render() {
return
}
}
// TODO - drag input values
// TODO - drop menu
// TODO - up/down arrow support
export class CSSUnitInput extends TextInputComponent {
// TODO
}
class CSSFilterInputComponent extends React.Component<{ filter: SyntheticCSSFilter, select: (filter: SyntheticCSSFilter) => any, rename: (filter: SyntheticCSSFilter, newName: string) => any }, any> {
render() {
const { filter } = this.props;
const { name, params } = filter;
return
{
this.props.select(filter);
}} />
{
if (option) this.props.rename(filter, (option as any).value);
}} />
}
renderInput(name: string, params: any[]) {
return {}} />
}
}
class BackgroundFillComponent extends React.Component<{ value: string, onClick?: () => any }, any> {
render() {
const { value } = this.props;
return
}
}