Skip to content

Commit

Permalink
feat: better drop cursor for crepe (#1378)
Browse files Browse the repository at this point in the history
* feat: 🎸 better drop cursor for crepe

* chore: fix

* chore: fix

* chore: optimize style
  • Loading branch information
Saul-Mirone authored Jun 17, 2024
1 parent 1ee21e8 commit c2dff9c
Show file tree
Hide file tree
Showing 20 changed files with 282 additions and 129 deletions.
5 changes: 2 additions & 3 deletions packages/components/src/list-item-block/component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Component } from 'atomico'
import { c, html, useHost, useLayoutEffect, useRef } from 'atomico'
import clsx from 'clsx'
import type { ListItemBlockConfig } from './config'

interface Attrs {
Expand Down Expand Up @@ -57,8 +56,8 @@ export const listItemComponent: Component<ListItemComponentProps> = ({
readonly,
}

return html`<host>
<li class=${clsx('list-item', selected && 'ProseMirror-selectednode')}>
return html`<host class=${selected && 'ProseMirror-selectednode'}>
<li class='list-item'>
<div class="label-wrapper" onclick=${onClickLabel} contenteditable="false">
${config?.renderLabel(labelProps)}
</div>
Expand Down
18 changes: 17 additions & 1 deletion packages/crepe/src/feature/block-edit/handle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ export class BlockHandleView implements PluginView {
this.#provider = new BlockProvider({
ctx,
content,
getPlacement: ({ active, blockDom }) => {
let totalDescendant = 0
active.node.descendants((node) => {
totalDescendant += node.childCount
})
const dom = active.el
const domRect = dom.getBoundingClientRect()
const handleRect = blockDom.getBoundingClientRect()
const style = window.getComputedStyle(dom)
const paddingTop = Number.parseInt(style.paddingTop, 10) || 0
const paddingBottom = Number.parseInt(style.paddingBottom, 10) || 0
const height = domRect.height - paddingTop - paddingBottom
const handleHeight = handleRect.height
return totalDescendant > 2 || handleHeight * 2 < height ? 'left-start' : 'left'
},
})
this.update()
}
Expand All @@ -43,9 +58,10 @@ export class BlockHandleView implements PluginView {
view.focus()

const { state, dispatch } = view
const active = this.#provider.activeNode
const active = this.#provider.active
if (!active)
return

const pos = active.$pos
const isNoneTextBlock = ['hr', 'image-block'].includes(active.node.type.name)
const nodeSize = isNoneTextBlock ? active.node.nodeSize : active.node.content.size
Expand Down
22 changes: 22 additions & 0 deletions packages/crepe/src/feature/cursor/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { cursor, dropCursorConfig } from '@milkdown/plugin-cursor'

import type { DefineFeature } from '../shared'

interface CursorConfig {
color: string | false
width: number
}
export type CursorFeatureConfig = CursorConfig

export const defineFeature: DefineFeature<CursorFeatureConfig> = (editor, config) => {
editor
.config((ctx) => {
ctx.update(dropCursorConfig.key, value => ({
...value,
class: 'crepe-drop-cursor',
width: config?.width ?? 4,
color: config?.color ?? false,
}))
})
.use(cursor)
}
19 changes: 0 additions & 19 deletions packages/crepe/src/feature/gap-cursor/index.ts

This file was deleted.

8 changes: 4 additions & 4 deletions packages/crepe/src/feature/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export enum CrepeFeature {
CodeMirror = 'code-mirror',
ListItem = 'list-item',
LinkTooltip = 'link-tooltip',
GapCursor = 'gap-cursor',
Cursor = 'cursor',
ImageBlock = 'image-block',
BlockEdit = 'block-edit',
Toolbar = 'toolbar',
Expand All @@ -22,7 +22,7 @@ export const defaultFeatures: Record<CrepeFeature, boolean> = {
[CrepeFeature.ListItem]: true,
[CrepeFeature.LinkTooltip]: true,
[CrepeFeature.ImageBlock]: true,
[CrepeFeature.GapCursor]: true,
[CrepeFeature.Cursor]: true,
[CrepeFeature.BlockEdit]: true,
[CrepeFeature.Placeholder]: true,
[CrepeFeature.Toolbar]: true,
Expand All @@ -47,8 +47,8 @@ export async function loadFeature(feature: CrepeFeature, editor: Editor, config?
const { defineFeature } = await import('./image-block')
return defineFeature(editor, config)
}
case CrepeFeature.GapCursor: {
const { defineFeature } = await import('./gap-cursor')
case CrepeFeature.Cursor: {
const { defineFeature } = await import('./cursor')
return defineFeature(editor, config)
}
case CrepeFeature.BlockEdit: {
Expand Down
50 changes: 37 additions & 13 deletions packages/crepe/src/theme/classic-dark/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@
color: var(--crepe-color-on-background);
background: var(--crepe-color-background);

input {
font-family: var(--crepe-font-default);
color: var(--crepe-color-on-background);
}

.ProseMirror-focused {
outline: none;
}
Expand All @@ -64,23 +59,49 @@
letter-spacing: 0.5px;

& > * {
margin: 20px 0 40px;
padding: 20px 0;
}

*::selection {
background: var(--crepe-color-selected);
//background: color-mix(in srgb, var(--crepe-color-selected), transparent 60%);
}

li.ProseMirror-selectednode {
outline: 10px solid var(--crepe-color-selected);
background: var(--crepe-color-selected);
outline: none;
//outline: 10px solid color-mix(in srgb, var(--crepe-color-selected), transparent 60%);
::selection {
background: transparent;
}
&::selection {
background: transparent;
}
}
li.ProseMirror-selectednode:after {
all: unset;
}

.ProseMirror-selectednode {
background: var(--crepe-color-selected);
outline: 10px solid var(--crepe-color-selected);
outline: none;
//outline: 10px solid var(--crepe-color-selected);
//background: color-mix(in srgb, var(--crepe-color-selected), transparent 60%);
//outline: 10px solid color-mix(in srgb, var(--crepe-color-selected), transparent 60%);
::selection {
background: transparent;
}
&::selection {
background: transparent;
}
}

&[data-dragging="true"] {
.ProseMirror-selectednode,
&::selection,
*::selection {
background: transparent;
}
}

img {
Expand Down Expand Up @@ -166,11 +187,16 @@
blockquote {
position: relative;
padding-left: 40px;
padding-top: 0;
padding-bottom: 0;
box-sizing: content-box;

&::before {
content: "";
width: 4px;
height: 100%;
left: 0;
top: 0;
bottom: 0;
position: absolute;
background: var(--crepe-color-selected);
border-radius: 100px;
Expand All @@ -194,10 +220,8 @@
}
}

.ProseMirror-gapcursor:after {
box-sizing: border-box;
border-top: 1px solid var(--crepe-color-on-background);
top: -40px;
ul,ol {
padding: 0;
}
}
}
45 changes: 37 additions & 8 deletions packages/crepe/src/theme/classic/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,49 @@
letter-spacing: 0.5px;

& > * {
margin: 20px 0 40px;
padding: 20px 0;
}

*::selection {
background: var(--crepe-color-selected);
//background: color-mix(in srgb, var(--crepe-color-selected), transparent 60%);
}

li.ProseMirror-selectednode {
outline: 10px solid var(--crepe-color-selected);
background: var(--crepe-color-selected);
outline: none;
//outline: 10px solid color-mix(in srgb, var(--crepe-color-selected), transparent 60%);
::selection {
background: transparent;
}
&::selection {
background: transparent;
}
}
li.ProseMirror-selectednode:after {
all: unset;
}

.ProseMirror-selectednode {
background: var(--crepe-color-selected);
outline: 10px solid var(--crepe-color-selected);
outline: none;
//outline: 10px solid var(--crepe-color-selected);
//background: color-mix(in srgb, var(--crepe-color-selected), transparent 60%);
//outline: 10px solid color-mix(in srgb, var(--crepe-color-selected), transparent 60%);
::selection {
background: transparent;
}
&::selection {
background: transparent;
}
}

&[data-dragging="true"] {
.ProseMirror-selectednode,
&::selection,
*::selection {
background: transparent;
}
}

img {
Expand Down Expand Up @@ -161,11 +187,16 @@
blockquote {
position: relative;
padding-left: 40px;
padding-top: 0;
padding-bottom: 0;
box-sizing: content-box;

&::before {
content: "";
width: 4px;
height: 100%;
left: 0;
top: 0;
bottom: 0;
position: absolute;
background: var(--crepe-color-selected);
border-radius: 100px;
Expand All @@ -189,10 +220,8 @@
}
}

.ProseMirror-gapcursor:after {
box-sizing: border-box;
border-top: 1px solid var(--crepe-color-on-background);
top: -40px;
ul,ol {
padding: 0;
}
}
}
3 changes: 1 addition & 2 deletions packages/crepe/src/theme/common/block-edit.css
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@

milkdown-slash-menu {
&[data-show='false'] {
opacity: 0;
display: none;
}
transition: opacity 0.2s;
position: absolute;
display: block;
width: 254px;
Expand Down
2 changes: 1 addition & 1 deletion packages/crepe/src/theme/common/code-mirror.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
milkdown-code-block {
display: block;
position: relative;
padding: 24px 40px;
padding: 24px 40px !important;
background: var(--crepe-color-surface);
margin: 20px 0;

Expand Down
16 changes: 16 additions & 0 deletions packages/crepe/src/theme/common/cursor.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@import "@milkdown/prose/gapcursor/style/gapcurosr.css";

.milkdown {
.crepe-drop-cursor {
background-color: color-mix(in srgb, var(--crepe-color-outline), transparent 50%);
opacity: 0.5;
transition: all 0.2s;
pointer-events: none
}

.ProseMirror-gapcursor:after {
box-sizing: border-box;
border-top: 1px solid var(--crepe-color-on-background);
top: -40px;
}
}
1 change: 0 additions & 1 deletion packages/crepe/src/theme/common/gap-cursor.css

This file was deleted.

2 changes: 1 addition & 1 deletion packages/crepe/src/theme/common/list-item.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.milkdown {
milkdown-list-item-block {
display: block;
margin-top: 10px;
padding: 5px 0;

& > .list-item {
display: flex;
Expand Down
2 changes: 1 addition & 1 deletion packages/crepe/src/theme/common/style.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@import "./prosemirror.css";
@import "./block-edit.css";
@import "./code-mirror.css";
@import "./gap-cursor.css";
@import "./cursor.css";
@import "./image-block.css";
@import "./link-tooltip.css";
@import "./list-item.css";
Expand Down
Loading

0 comments on commit c2dff9c

Please sign in to comment.