1.3.1 • Published 8 months ago

@raisins/react v1.3.1

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

Raisins React

Raisins in a WYSIWYG visual editor for HTML and web components. This is the react package, which handles the state behind the visual rendering experience.

Installation

npm i @raisins/react

Examples

Raisins React comes with many controllers and molecules to help build a visual editor for your HTML. By using each of them together, you are able to control each different piece of functionality to include or exclude from the editing experience.

Base Example

  • The base example contains a canvas with some basic text editability. It can be combined with the examples below to provide a more complete editability experience.
  const WidgetScope = createScope<{
    startingHtml: string;
    startingPackages: Module[];
  }>({
    startingHtml: '',
    startingPackages: [] as Module[],
  });

  const ConfigMolecule = molecule<Partial<RaisinConfig>>((_, getScope) => {
    const widgetScope = getScope(WidgetScope);
    return {
      HTMLAtom: atom(widgetScope.startingHtml),
      PackagesAtom: atom(widgetScope.startingPackages),
      uiWidgetsAtom: atom({}),
    };
  });

  const startingHtml = `
    <h1>My Heading</h1>
    <p style="width:200px;height:100px;">My first paragraph</p>
    <p style="width:200px;height:100px;">My second paragraph</p>
`;

  const Editor = () => {
    useHotkeys();
    return (
      <div style={{ display: 'flex' }}>
        <div style={{ flex: 0.7 }}>
          <BasicCanvasController />
        </div>
        <div style={{ flex: 0.3 }}>
          <AttributeEditor />
        </div>
      </div>
    );
  };

  const AttributeEditor = () => {
    useMolecule(CanvasSelectionMolecule);
    useMolecule(CanvasHoveredMolecule);
    return (
      <SelectedNodeController
        HasSelectionComponent={AttributesController}
      ></SelectedNodeController>
    );
  };

export function BaseExample() {
  return (
    <ScopeProvider
      scope={WidgetScope}
      value={{
        startingHtml,
        startingPackages: [],
      }}
    >
      <RaisinsProvider molecule={ConfigMolecule}>
        <CanvasProvider>
          <Editor />
        </CanvasProvider>
      </RaisinsProvider>
    </ScopeProvider>
  );
}

External HTML Control

  const Editor = () => {
    useHotkeys();
    const { HTMLAtom } = useMolecule(ConfigMolecule);
    const [html, setHtml] = useAtom(HTMLAtom!);

    return (
      <>
        <textarea
          value={html}
          onInput={(e) => setHtml((e.target as HTMLTextAreaElement).value)}
          rows={10}
          style={{ width: '500px' }}
        />
        <div style={{ display: 'flex' }}>
          <div style={{ flex: 0.7 }}>
            <BasicCanvasController />
          </div>
          <div style={{ flex: 0.3 }}>
            <AttributeEditor />
          </div>
        </div>
      </>
    );
  };

Canvas Toolbars

const ToolbarMolecule = molecule((getMol) => {
  return {
    ...getMol(HoveredNodeMolecule),
    ...getMol(SelectedNodeMolecule),
    ...getMol(HistoryMolecule),
    ...getMol(CanvasStyleMolecule),
    ...getMol(CanvasSelectionMolecule),
    ...getMol(CanvasHoveredMolecule),
    ...getMol(ComponentModelMolecule),
  };
});

const Toolbars = () => {
  const {
    HoveredNodeAtom,
    SelectedNodeAtom,
    SelectedRectAtom,
    HoveredRectAtom,
    ComponentModelAtom,
  } = useMolecule(ToolbarMolecule);

  const hoveredNode = useAtomValue(HoveredNodeAtom) as RaisinElementNode;
  const selectedNode = useAtomValue(SelectedNodeAtom) as RaisinElementNode;

  const { getComponentMeta } = useAtomValue(ComponentModelAtom);
  const hoveredTitle = getComponentMeta(hoveredNode?.tagName).title;
  const selectedTitle = getComponentMeta(selectedNode?.tagName).title;
  return (
    <>
      <PositionedToolbar rectAtom={HoveredRectAtom}>
        Hovered - {hoveredTitle}
      </PositionedToolbar>
      <PositionedToolbar rectAtom={SelectedRectAtom}>
        Selected - {selectedTitle}
        <SelectedNodeRichTextEditor />
      </PositionedToolbar>
    </>
  );
};

  const Editor = () => {
    useHotkeys();
    const { EditSelectedNodeAtom } = useMolecule(EditSelectedMolecule);
    return (
      <>
        <NodeScopeProvider nodeAtom={EditSelectedNodeAtom}>
          <Toolbars />
        </NodeScopeProvider>
        <div style={{ display: 'flex' }}>
          <div style={{ flex: 0.7 }}>
            <BasicCanvasController />
          </div>
          <div style={{ flex: 0.3 }}>
            <AttributeEditor />
          </div>
        </div>
      </>
    );
  };

Layers

const Label: CSSProperties = {
  cursor: 'pointer',
  lineHeight: '28px',
};

const Layer: CSSProperties = {
  position: 'relative',
  userSelect: 'none',
  cursor: 'pointer',
  padding: '10px 0',
  outlineOffset: '-2px',
};

const SelectedLayer: CSSProperties = {
  background: 'green',
};

const SlotContainer: CSSProperties = {
  marginLeft: '3px',
  display: 'flex',
  padding: '0 0 3px 0',
};

const SlotName: CSSProperties = {
  writingMode: 'vertical-lr',
  textOrientation: 'sideways',
  fontSize: '0.7em',
  padding: '5px 0 5px 2px',
  color: 'grey',
  background: 'rgba(0, 0, 0, 0.1)',
};

const SlotChildren: CSSProperties = {
  width: '100%',
};
const TitleBar: CSSProperties = {
  display: 'flex',
  justifyContent: 'space-between',
};
type Atoms = {
  slot: string,
  slotDetails: Atom<Slot>,
  slotName: string,
  childrenInSlot: WritableAtom<
    PrimitiveAtom<RaisinNode>[],
    PrimitiveAtom<RaisinNode>,
    void
  >,
};

const LayersController = () => {
  const atoms = useMolecule(LayersMolecule);
  const hasChildren = useAtomValue(atoms.RootHasChildren);

  return (
    <NodeScopeProvider nodeAtom={atoms.RootNodeAtom}>
      {hasChildren && (
        <SlotScopeProvider slot="">
          <NodeChildrenEditor Component={ElementLayer} />
        </SlotScopeProvider>
      )}
    </NodeScopeProvider>
  );
};
function ElementLayer() {
  const {
    isNodeAnElement,
    isSelectedForNode,
    nameForNode,
    nodeHovered,
    setSelectedForNode,
    allSlotsForNode,
  } = useMolecule(NodeMolecule);

  const setSelected = useSetAtom(setSelectedForNode);
  const isAnElement = useAtomValue(isNodeAnElement);
  const isSelected = useAtomValue(isSelectedForNode);
  const [isHovered, setHovered] = useAtom(nodeHovered);
  const slots = useAtomValue(allSlotsForNode);
  const title = useAtomValue(nameForNode);
  // Don't render non-element layers
  if (!isAnElement) return <></>;

  const name = (
    <div style={TitleBar} onClick={setSelected} onMouseOver={setHovered}>
      <div style={Label}>{title}</div>
    </div>
  );

  const hasSlots = slots?.length > 0;
  const style = {
    ...Layer,
    ...(isSelected ? SelectedLayer : {}),
    ...(isHovered ? { outline: '1px dashed green' } : {}),
  };

  return (
    <div data-element style={style}>
      {!hasSlots && name}
      {hasSlots && (
        <div>
          {name}
          {hasSlots && (
            <div>
              <SlotChildrenController Component={SlotWidget} />
            </div>
          )}
        </div>
      )}
    </div>
  );
}

function SlotWidget() {
  const atoms = useMolecule(SlotMolecule);
  const childNodes = useAtomValue(atoms.childrenInSlot);

  const slotDetails = useAtomValue(atoms.slotDetails);
  const slotWidget = slotDetails.editor;
  const hasEditor = slotWidget === 'inline';
  const isEmpty = (childNodes?.length ?? 0) <= 0;

  return (
    <>
      <div style={SlotContainer}>
        <div style={SlotName}>
          {slotDetails.title ?? slotDetails.name} ({childNodes.length})
        </div>
        {hasEditor && <NodeRichTextController />}
        {!hasEditor && (
          // Block Editor
          <>
            {!isEmpty && (
              <div style={SlotChildren}>
                <PlopTarget idx={0} slot={slotDetails.name} />
                <ChildrenEditorForAtoms
                  childAtoms={childNodes}
                  Component={({ idx }) => (
                    <SlotChildSimple idx={idx} atoms={atoms} />
                  )}
                />
              </div>
            )}
          </>
        )}
      </div>
    </>
  );
}

const SlotChild: React.FC<{ idx?: number, atoms: Atoms }> = ({
  idx,
  atoms,
}) => {
  return (
    <>
      <ElementLayerName />
      <PlopTarget idx={idx ?? 0} slot={atoms.slotName} />
    </>
  );
};
const Editor = () => {
  useHotkeys();
  return (
    <div style={{ display: 'flex' }}>
      <div style={{ flex: 0.3 }}>
        <LayersController />
      </div>
      <div style={{ flex: 0.4 }}>
        <BasicCanvasController />
      </div>
      <div style={{ flex: 0.3 }}>
        <AttributeEditor />
      </div>
    </div>
  );
};

Layers With Buttons

type Atoms = {
  slot: string,
  slotDetails: Atom<Slot>,
  slotName: string,
  childrenInSlot: WritableAtom<
    PrimitiveAtom<RaisinNode>[],
    PrimitiveAtom<RaisinNode>,
    void
  >,
};

function ElementLayer() {
  const {
    duplicateForNode,
    isNodeAnElement,
    isNodePicked,
    isSelectedForNode,
    nameForNode,
    nodeHovered,
    removeForNode,
    setSelectedForNode,
    allSlotsForNode,
    togglePickNode,
  } = useMolecule(NodeMolecule);

  const atoms = useMolecule(LayersMolecule);
  const setSelected = useSetAtom(setSelectedForNode);
  const isAnElement = useAtomValue(isNodeAnElement);
  const isSelected = useAtomValue(isSelectedForNode);
  const isPicked = useAtomValue(isNodePicked);
  const [isHovered, setHovered] = useAtom(nodeHovered);
  const slots = useAtomValue(allSlotsForNode);

  const removeNode = useSetAtom(removeForNode);
  const duplicate = useSetAtom(duplicateForNode);
  const title = useAtomValue(nameForNode);
  const moveNode = useSetAtom(togglePickNode);
  const isPlopping = useAtomValue(atoms.PloppingIsActive);
  const canMove = isPicked || !isPlopping;
  // Don't render non-element layers
  if (!isAnElement) return <></>;

  const name = (
    <div style={TitleBar} onClick={setSelected} onMouseOver={setHovered}>
      <div style={Label}>
        {title} {isPicked && ' Moving...'}
      </div>
      <div>
        <button onClick={moveNode} disabled={!canMove}>
          {isPicked ? 'Cancel move' : 'Move'}
        </button>
        <button onClick={duplicate} disabled={isPlopping}>
          Dupe
        </button>
        <button onClick={removeNode} disabled={isPlopping}>
          Delete
        </button>
      </div>
    </div>
  );

  const hasSlots = slots?.length > 0;
  const style = {
    ...Layer,
    ...(isSelected ? SelectedLayer : {}),
    ...(isHovered ? { outline: '1px dashed green' } : {}),
  };

  return (
    <div data-element style={style}>
      {!hasSlots && name}
      {hasSlots && (
        <div>
          {name}
          {hasSlots && (
            <div>
              <SlotChildrenController Component={SlotWidget} />
            </div>
          )}
        </div>
      )}
    </div>
  );
}

function SlotWidget() {
  const atoms = useMolecule(SlotMolecule);
  const childNodes = useAtomValue(atoms.childrenInSlot);

  const slotDetails = useAtomValue(atoms.slotDetails);
  const slotWidget = slotDetails.editor;
  const hasEditor = slotWidget === 'inline';
  const isEmpty = (childNodes?.length ?? 0) <= 0;

  return (
    <>
      <div style={SlotContainer}>
        <div style={SlotName}>
          {slotDetails.title ?? slotDetails.name} ({childNodes.length})
        </div>
        {hasEditor && <NodeRichTextController />}
        {!hasEditor && (
          // Block Editor
          <>
            {!isEmpty && (
              <div style={SlotChildren}>
                <PlopTarget idx={0} slot={slotDetails.name} />
                <ChildrenEditorForAtoms
                  childAtoms={childNodes}
                  Component={({ idx }) => <SlotChild idx={idx} atoms={atoms} />}
                />
              </div>
            )}
          </>
        )}
      </div>
    </>
  );
}

const SlotChild: React.FC<{ idx?: number, atoms: Atoms }> = ({
  idx,
  atoms,
}) => {
  return (
    <>
      <ElementLayer />
      <PlopTarget idx={idx ?? 0} slot={atoms.slotName} />
    </>
  );
};
function PlopTarget({ idx, slot }: { idx: number, slot: string }) {
  const { canPlopHereAtom, plopNodeHere } = useMolecule(NodeMolecule);
  const canPlop = useAtomValue(canPlopHereAtom);
  const plopNode = useSetAtom(plopNodeHere);
  const plop = useCallback(() => plopNode({ idx, slot }), [idx, slot]);

  const isPloppable = canPlop({ slot, idx });
  if (!isPloppable) {
    return <></>;
  }
  return (
    <div style={{ border: '1px dashed red' }}>
      Position {idx} in {slot || 'content'}
      <button onClick={plop}>Plop</button>
    </div>
  );
}

const Editor = () => {
  useHotkeys();
  return (
    <div style={{ display: 'flex' }}>
      <div style={{ flex: 0.3 }}>
        <LayersController />
      </div>
      <div style={{ flex: 0.4 }}>
        <BasicCanvasController />
      </div>
      <div style={{ flex: 0.3 }}>
        <AttributeEditor />
      </div>
    </div>
  );
};

Custom Components

const Editor = () => {
  useHotkeys();
  return (
    <div style={{ display: 'flex' }}>
      <div style={{ flex: 0.3 }}>
        <LayersController />
      </div>
      <div style={{ flex: 0.4 }}>
        <BasicCanvasController />
      </div>
      <div style={{ flex: 0.3 }}>
        <AttributeEditor />
      </div>
    </div>
  );
};

const html =
  startingHtml +
  `<sqm-timeline icon="circle">
<sqm-timeline-entry reward="$50" unit="visa giftcard" desc="Your friend purchases a Business plan" icon="circle">
</sqm-timeline-entry>
<sqm-timeline-entry reward="$200" unit="visa giftcard" desc="Our sales team qualifies your friend as a good fit for our Enterprise plan" icon="circle">
</sqm-timeline-entry>
<sqm-timeline-entry reward="$1000" unit="visa giftcard" desc="Your friend purchases an Enterprise plan" icon="circle">
</sqm-timeline-entry></sqm-timeline>`;

export function CustomComponents() {
  return (
    <ScopeProvider
      scope={WidgetScope}
      value={{
        startingHtml: html,
        startingPackages: [
          {
            package: '@saasquatch/mint-components',
            version: '1.6.x',
          },
        ],
      }}
    >
      <RaisinsProvider molecule={ConfigMolecule}>
        <CanvasProvider>
          <Editor />
        </CanvasProvider>
      </RaisinsProvider>
    </ScopeProvider>
  );
}

Full Example

// Config
// ...WidgetScope, ConfigMolecule

// Layers
// ...ElementLayer, SlotWidget, SlotChild, PlopTarget

  const Editor = () => {
    useHotkeys();
    const { EditSelectedNodeAtom } = useMolecule(EditSelectedMolecule);
    const { HTMLAtom } = useMolecule(ConfigMolecule);
    const [html, setHtml] = useAtom(HTMLAtom!);

    return (
      <>
        <textarea
          value={html}
          onInput={(e) => setHtml((e.target as HTMLTextAreaElement).value)}
          rows={10}
          style={{ width: '500px' }}
        />
        <div style={{ display: 'flex' }}>
          <div style={{ flex: 0.3 }}>
            <LayersController />
          </div>
          <div style={{ flex: 0.4 }}>
            <NodeScopeProvider nodeAtom={EditSelectedNodeAtom}>
              <Toolbars />
            </NodeScopeProvider>
            <BasicCanvasController />
          </div>
          <div style={{ flex: 0.3 }}>
            <AttributeEditor />
          </div>
        </div>
      </>
    );
  };

  const AttributeEditor = () => {
    useMolecule(CanvasSelectionMolecule);
    useMolecule(CanvasHoveredMolecule);
    useMolecule(CanvasPickAndPlopMolecule);
    return (
      <SelectedNodeController
        HasSelectionComponent={AttributesController}
      ></SelectedNodeController>
    );
  };

  const html =
    startingHtml +
    `<sqm-timeline icon="circle">
<sqm-timeline-entry reward="$50" unit="visa giftcard" desc="Your friend purchases a Business plan" icon="circle">
</sqm-timeline-entry>
<sqm-timeline-entry reward="$200" unit="visa giftcard" desc="Our sales team qualifies your friend as a good fit for our Enterprise plan" icon="circle">
</sqm-timeline-entry>
<sqm-timeline-entry reward="$1000" unit="visa giftcard" desc="Your friend purchases an Enterprise plan" icon="circle">
</sqm-timeline-entry></sqm-timeline>`;

export function FullExample() {
  return (
    <ScopeProvider
      scope={WidgetScope}
      value={{
        startingHtml: html,
        startingPackages: [
          {
            package: '@saasquatch/mint-components',
            version: '1.6.x',
          },
        ],
      }}
    >
      <RaisinsProvider molecule={ConfigMolecule}>
        <CanvasProvider>
          <Editor />
        </CanvasProvider>
      </RaisinsProvider>
    </ScopeProvider>
  );
}
1.3.1

8 months ago

1.3.1-3

8 months ago

1.3.1-2

8 months ago

1.3.1-1

8 months ago

1.3.1-0

8 months ago

1.3.0-0

8 months ago

1.3.0-2

8 months ago

1.3.0-1

8 months ago

1.2.2-18

8 months ago

1.3.0

8 months ago

1.2.2-1

12 months ago

1.2.2-3

11 months ago

1.2.2-2

11 months ago

1.2.2

11 months ago

1.2.1

12 months ago

1.2.2-5

11 months ago

1.2.2-4

11 months ago

1.2.2-7

11 months ago

1.2.2-6

11 months ago

1.2.2-9

11 months ago

1.2.2-8

11 months ago

1.2.2-12

11 months ago

1.2.2-11

11 months ago

1.2.2-14

11 months ago

1.2.2-13

11 months ago

1.2.2-10

11 months ago

1.2.2-16

10 months ago

1.2.2-15

11 months ago

1.2.2-17

10 months ago

1.2.0

1 year ago

1.1.6-3

1 year ago

1.1.6-2

1 year ago

1.1.6-1

1 year ago

1.1.5

1 year ago

1.1.5-0

1 year ago

1.1.4-9

1 year ago

1.1.4-8

1 year ago

1.1.4-10

1 year ago

1.1.4

1 year ago

1.1.4-7

1 year ago

1.1.4-6

2 years ago

1.1.4-5

2 years ago

1.1.4-4

2 years ago

1.1.4-3

2 years ago

1.1.4-2

2 years ago

1.1.4-1

2 years ago

1.1.4-0

2 years ago

1.1.3

2 years ago

1.1.3-4

2 years ago

1.1.3-3

2 years ago

1.1.2

2 years ago

1.1.3-2

2 years ago

1.1.3-1

2 years ago

1.1.3-0

2 years ago

1.1.1

2 years ago

1.1.1-1

2 years ago

1.1.1-0

2 years ago

1.0.2-14

3 years ago

1.0.2-13

3 years ago

1.0.2-12

3 years ago

1.0.2-11

3 years ago

1.0.2-10

3 years ago

1.0.2-7

3 years ago

1.0.2-6

3 years ago

1.0.2-9

3 years ago

1.0.2-8

3 years ago

1.0.2-3

3 years ago

1.0.2-2

3 years ago

1.0.2-5

3 years ago

1.0.2-4

3 years ago

1.0.2-1

3 years ago

1.0.2-0

3 years ago

1.1.0

3 years ago

1.0.1

3 years ago

0.85.0

3 years ago

0.62.0

3 years ago

0.43.0

3 years ago

0.81.0

3 years ago

0.66.0-1

3 years ago

0.66.0-2

3 years ago

0.66.0-3

3 years ago

0.59.0

3 years ago

0.66.0-4

3 years ago

0.78.0

3 years ago

0.55.0

3 years ago

0.66.0-0

3 years ago

0.66.0-5

3 years ago

0.66.0-6

3 years ago

0.74.0

3 years ago

0.51.0

3 years ago

0.70.0

3 years ago

0.40.0-0

3 years ago

0.48.0

3 years ago

0.67.0

3 years ago

0.44.0

3 years ago

0.85.0-0

3 years ago

0.68.0-0

3 years ago

0.63.0

3 years ago

0.86.0

3 years ago

0.40.0

3 years ago

0.82.0

3 years ago

0.79.0

3 years ago

0.56.0

3 years ago

0.90.0

3 years ago

0.48.0-0

3 years ago

0.75.0

3 years ago

0.52.0

3 years ago

0.71.0

3 years ago

0.49.0

3 years ago

0.68.0

3 years ago

0.45.0

3 years ago

1.0.0

3 years ago

0.87.0

3 years ago

0.64.0

3 years ago

0.41.0

3 years ago

0.83.0

3 years ago

0.60.0

3 years ago

0.38.0

3 years ago

0.57.0

3 years ago

0.78.0-0

3 years ago

0.53.0

3 years ago

0.76.0

3 years ago

0.69.0-0

3 years ago

0.69.0-1

3 years ago

0.72.0

3 years ago

0.88.0

3 years ago

0.46.0

3 years ago

0.69.0

3 years ago

0.59.0-0

3 years ago

0.84.0

3 years ago

0.42.0

3 years ago

0.65.0

3 years ago

0.80.0

3 years ago

0.61.0

3 years ago

0.39.0

3 years ago

0.77.0

3 years ago

0.58.0

3 years ago

0.39.0-0

3 years ago

0.73.0

3 years ago

0.54.0

3 years ago

0.50.0

3 years ago

0.89.0

3 years ago

0.66.0

3 years ago

0.47.0

3 years ago

0.63.0-0

3 years ago

0.37.0

3 years ago

0.36.0

3 years ago

0.35.0

3 years ago

0.34.0

3 years ago

0.33.0

3 years ago

0.32.0

3 years ago

0.31.0

3 years ago

0.30.0

3 years ago

0.29.0

3 years ago

0.28.0

3 years ago

0.27.0

3 years ago

0.26.0

3 years ago

0.25.0

3 years ago

0.24.0

3 years ago

0.23.0

3 years ago

0.22.0

3 years ago

0.21.0

3 years ago

0.20.0

3 years ago

0.19.0

3 years ago

0.18.0

3 years ago

0.17.0

3 years ago

0.16.0

3 years ago

0.15.0

3 years ago

0.15.0-1

3 years ago

0.15.0-0

3 years ago

0.14.0

3 years ago

0.13.0

3 years ago

0.12.0

3 years ago

0.11.0-1

3 years ago

0.11.0-0

3 years ago

0.11.0

3 years ago

0.10.0

3 years ago

0.9.0

3 years ago

0.8.0

3 years ago

0.7.0

3 years ago

0.6.0

3 years ago

0.5.0

3 years ago

0.4.0

3 years ago

0.3.0

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago