Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="stencil-container"></div>
<div id="paper-container"></div>
<div id="toolbar-container">
  Command Manager
</div>
<a id="logo-link" target="_blank" href="https://www.jointjs.com">
  <img id="logo" src="https://assets.codepen.io/7589991/jointjs-logo.svg" width="200" height="50"></img>
</a>
              
            
!

CSS

              
                #stencil-container {
  position: absolute;
  left: 0;
  top: 0;
  width: 100px;
  bottom: 0;
}

#paper-container {
  position: absolute;
  right: 0;
  top: 0;
  left: 100px;
  bottom: 0;

  .selection-wrapper[data-selection-length="1"] {
    display: block;

    ~ .selection-box {
      display: none;
    }
  }
}

#toolbar-container {
  position: absolute;
  top: 20px;
  right: 20px;
  font-family: sans-serif;
  padding: 4px;
  border: 1px solid #d3d3d3;
  background: #eee;

  .joint-toolbar {
    margin: 4px 0 0 0;
    padding: 4px;

    [data-type="label"] {
      width: 100px;
      text-align: center;
      display: inline-block;
    }
  }
}

#logo {
  position: absolute;
  bottom: 20px;
  right: 0;
}

/* Hide Unknown Value Icon */
.select-box-option:first-child {
  display: none;
}

              
            
!

JS

              
                const { dia, ui, shapes } = joint;

// Paper

const graph = new dia.Graph({}, { cellNamespace: shapes });
const paper = new dia.Paper({
  model: graph,
  cellViewNamespace: shapes,
  width: "100%",
  height: "100%",
  gridSize: 20,
  drawGrid: { name: "mesh" },
  async: true,
  sorting: dia.Paper.sorting.APPROX,
  background: { color: "#F3F7F6" },
  restrictTranslate: true,
  clickThreshold: 10
});

document.getElementById("paper-container").appendChild(paper.el);

// Stencil

const stencil = new ui.Stencil({
  paper,
  usePaperGrid: true,
  width: 100,
  height: "100%",
  paperOptions: () => {
    return {
      model: new dia.Graph({}, { cellNamespace: shapes }),
      cellViewNamespace: shapes,
      background: {
        color: "#FCFCFC"
      }
    };
  },
  layout: {
    columns: 1,
    rowHeight: "compact",
    rowGap: 10,
    columnWidth: 100,
    marginY: 10,
    // reset defaults
    resizeToFit: false,
    dx: 0,
    dy: 0
  }
});

stencil.render();
document.getElementById("stencil-container").appendChild(stencil.el);

stencil.load([
  {
    type: "standard.Rectangle",
    size: { width: 80, height: 60 },
    attrs: {
      body: {
        stroke: "#222138",
        fill: "#f6f6f6"
      }
    }
  }
]);

// Selection

const selection = new ui.Selection({
  paper,
  useModelGeometry: true,
  theme: "material",
  boxContent: false
});

selection.removeHandle("resize");
selection.removeHandle("rotate");

paper.on("blank:pointerdown", function (evt, x, y) {
  selection.startSelecting(evt);
  focus();
});

paper.on("element:pointerclick", function (elementView) {
  selection.collection.reset([elementView.model]);
  focus();
});

// Example

const rectangle1 = new shapes.standard.Rectangle({
  position: { x: 100, y: 100 },
  size: { width: 180, height: 80 },
  attrs: {
    label: {
      text:
        "Select the element and\n use up, down, left, right keys\nto move it",
      fontSize: 12,
      fontFamily: "sans-serif",
      lineHeight: 20
    },
    body: {
      stroke: "#222138",
      fill: "#f6f6f6"
    }
  }
});

const rectangle2 = new shapes.standard.Rectangle({
  position: { x: 250, y: 250 },
  size: { width: 180, height: 80 },
  attrs: {
    label: {
      text: "Observe how\nthe command manager\nconsolidates changes",
      fontSize: 12,
      fontFamily: "sans-serif",
      lineHeight: 20
    },
    body: {
      stroke: "#222138",
      fill: "#f6f6f6"
    }
  }
});

graph.addCells([rectangle1, rectangle2]);

// Command Manager

const history = new dia.CommandManager({
  graph,
  applyOptionsList: ["skipHistory"]
});

history.on("stack:undo stack:redo", function () {
  selection.collection.reset(
    selection.collection.filter((cell) => graph.getCell(cell))
  );
});

history.on("stack:push", (batch) => {
  if (batch[batch.length - 1]?.options?.skipHistory) {
    history.squashUndo(2);
  }
});

// Toolbar

const toolbar = new ui.Toolbar({
  autoToggle: true,
  tools: [
    { type: "label", name: "undo-label" },
    "undo",
    "redo",
    { type: "label", name: "redo-label" }
  ],
  references: {
    commandManager: history
  }
});

document.getElementById("toolbar-container").appendChild(toolbar.render().el);

function updateToolbar() {
  toolbar.getWidgetByName(
    "undo-label"
  ).el.textContent = `${history.undoStack.length} action(s) to undo`;
  toolbar.getWidgetByName(
    "redo-label"
  ).el.textContent = `${history.redoStack.length} action(s) to redo`;
}

updateToolbar();

history.on("stack", () => updateToolbar());

// Keyboard

const keyboard = new ui.Keyboard();
const MOVE_STEP = 20;

keyboard.on({
  up: function (evt: dia.Event) {
    // Prevent Default Scrolling
    evt.preventDefault();
    moveCells(selection.collection.toArray(), 0, -MOVE_STEP);
  },
  down: function (evt: dia.Event) {
    evt.preventDefault();
    moveCells(selection.collection.toArray(), 0, MOVE_STEP);
  },
  left: function (evt: dia.Event) {
    evt.preventDefault();
    moveCells(selection.collection.toArray(), -MOVE_STEP, 0);
  },
  right: function (evt: dia.Event) {
    evt.preventDefault();
    moveCells(selection.collection.toArray(), MOVE_STEP, 0);
  }
});

let _movedElementsHash = "";

graph.on("change", () => (_movedElementsHash = ""));

function moveCells(cells: dia.Cell[], dx: number, dy: number) {
  const elements = cells.filter((cell) => cell.isElement()) as dia.Element[];
  const hash = elements
    .map((el) => el.id)
    .sort()
    .join(" ");
  const movedPreviously = hash === _movedElementsHash;

  graph.startBatch("shift-selection");
  elements.forEach((el) =>
    el.translate(dx, dy, { skipHistory: movedPreviously })
  );
  graph.stopBatch("shift-selection");

  _movedElementsHash = hash;
}

// Make sure the codepen iframe gets focus (for the purpose of the code pen demo only)
function focus() {
  document.getElementById("logo-link").focus();
}

              
            
!
999px

Console