Skip to content

Commit

Permalink
feat: add link tooltip (#1188)
Browse files Browse the repository at this point in the history
* feat: add link tooltip component

* chore: optimize

* chore: auto focus edit input

* feat: optimize style
  • Loading branch information
Saul-Mirone authored Dec 11, 2023
1 parent 9f73f77 commit 1e8a627
Show file tree
Hide file tree
Showing 25 changed files with 978 additions and 111 deletions.
1 change: 1 addition & 0 deletions e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@milkdown/plugin-history": "workspace:*",
"@milkdown/plugin-listener": "workspace:*",
"@milkdown/plugin-math": "workspace:*",
"@milkdown/plugin-tooltip": "workspace:*",
"@milkdown/preset-commonmark": "workspace:*",
"@milkdown/preset-gfm": "workspace:*",
"@milkdown/prose": "workspace:*",
Expand Down
5 changes: 5 additions & 0 deletions e2e/src/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,9 @@ export const cards: CardComponentProps[] = [
description: 'List item block component',
link: '/list-item-block/',
},
{
title: 'Link Tooltip',
description: 'Link tooltip component',
link: '/link-tooltip/',
},
]
2 changes: 1 addition & 1 deletion e2e/src/image-block/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ milkdown-image-block > .image-edit .link-importer .placeholder .text {
milkdown-image-block > .image-edit .confirm {
background: darkgray;
color: antiquewhite;
height: 40px;
line-height: 40px;
padding: 0 24px;
border-radius: 100px;
font-size: 14px;
Expand Down
21 changes: 21 additions & 0 deletions e2e/src/link-tooltip/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Link Tooltip</title>
</head>
<body>
<div id="ui" class="mx-5 my-2 flex justify-between items-center">
<div class="flex gap-2">
<button id="inspect" class="text-gray-50 rounded px-4 py-2 bg-nord10 hover:bg-nord9">
inspect
</button>
<button id="log" class="text-gray-50 rounded px-4 py-2 bg-nord10 hover:bg-nord9">
log
</button>
</div>
</div>
<div id="app"></div>
<script type="module" src="/link-tooltip/main.ts"></script>
</body>
</html>
89 changes: 89 additions & 0 deletions e2e/src/link-tooltip/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* Copyright 2021, Milkdown by Mirone. */

import { Editor, defaultValueCtx, editorViewCtx, rootCtx } from '@milkdown/core'
import { nord } from '@milkdown/theme-nord'
import { commonmark, linkSchema } from '@milkdown/preset-commonmark'
import {
configureLinkTooltip,
linkTooltipAPI,
linkTooltipPlugin,
linkTooltipState,
} from '@milkdown/components/link-tooltip'
import { TooltipProvider, tooltipFactory } from '@milkdown/plugin-tooltip'
import type { EditorState } from '@milkdown/prose/state'
import type { EditorView } from '@milkdown/prose/view'

import type { Ctx } from '@milkdown/ctx'
import { setup } from '../utils'

import '@milkdown/theme-nord/style.css'

import '../style.css'

import './style.css'

const markdown = `
# Link tooltip
Is this the real [life](https://en.wikipedia.org/wiki/Life)? Is this just [fantasy](https://en.wikipedia.org/wiki/fantasy)?
Caught in a [landslide](https://en.wikipedia.org/wiki/landslide), no escape from [reality](https://en.wikipedia.org/wiki/reality).
`

const insertLinkTooltip = tooltipFactory('CREATE_LINK')

function tooltipPluginView(ctx: Ctx) {
return (_view: EditorView) => {
const content = document.createElement('div')
const provider = new TooltipProvider({
content,
shouldShow: (view: EditorView) => {
const { selection, doc } = view.state
const has = doc.rangeHasMark(selection.from, selection.to, linkSchema.type(ctx))
if (has || selection.empty)
return false

return true
},
})

content.textContent = '🔗'
content.className = 'link-insert-button'
content.onmousedown = (e: MouseEvent) => {
e.preventDefault()
const view = ctx.get(editorViewCtx)
const { selection } = view.state
ctx.get(linkTooltipAPI.key).addLink(selection.from, selection.to)
provider.hide()
}

return {
update: (updatedView: EditorView, prevState: EditorState) => {
if (ctx.get(linkTooltipState.key).mode === 'edit')
return
provider.update(updatedView, prevState)
},
destroy: () => {
provider.destroy()
content.remove()
},
}
}
}

setup(() => {
return Editor.make()
.config((ctx) => {
ctx.set(rootCtx, document.getElementById('app'))
ctx.set(defaultValueCtx, markdown)
ctx.set(insertLinkTooltip.key, {
view: tooltipPluginView(ctx),
})
configureLinkTooltip(ctx)
})
.config(nord)
.use(commonmark)
.use(linkTooltipPlugin)
.use(insertLinkTooltip)
.create()
})
97 changes: 97 additions & 0 deletions e2e/src/link-tooltip/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
.link-insert-button {
background: bisque;
cursor: pointer;
padding: 4px 16px;
border-radius: 16px;
&:hover {
background: burlywood;
}
}

milkdown-link-preview {
& > .link-preview {
display: flex;
justify-content: center;
padding: 8px;
background: bisque;
gap: 8px;
border-radius: 16px;
cursor: pointer;

& > .link-display:hover:before {
display: block;
}

& > .link-display:before {
content: 'click to copy link';
position: absolute;
transform: translate(50%, -130%);
padding: 6px 10px;
font-size: 12px;
border-radius: 10px;
background: #000;
color: #fff;
text-align: center;

display: none;
}

& > .link-icon {
& > svg {
width: 14px;
height: 14px;
}
padding: 5px;
}

& > .link-display {
width: 240px;
line-height: 24px;
overflow: hidden;
text-overflow: ellipsis;
font-size: 14px;
&:hover {
text-decoration: underline;
}
}

& > .button {
& > svg {
width: 14px;
height: 14px;
}
padding: 5px;
border-radius: 99px;
&:hover {
background: burlywood;
}
}
}
}

milkdown-link-edit {
& > .link-edit {
display: flex;
padding: 8px 12px;
background: bisque;
gap: 8px;
border-radius: 16px;

& > .input-area {
outline: none;
background: transparent;
width: 200px;
font-size: 14px;
}

& > .button {
cursor: pointer;
border-radius: 8px;
font-size: 12px;
padding: 4px;
&:hover {
background: burlywood;
}
}
}
}
1 change: 1 addition & 0 deletions e2e/vite.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default defineConfig({
'image-block': resolve(__dirname, 'src/image-block/index.html'),
'code-block': resolve(__dirname, 'src/code-block/index.html'),
'list-item-block': resolve(__dirname, 'src/list-item-block/index.html'),
'link-tooltip': resolve(__dirname, 'src/link-tooltip/index.html'),
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"commit": "git-cz"
},
"devDependencies": {
"@antfu/eslint-config": "^2.1.1",
"@antfu/eslint-config": "^2.3.4",
"@changesets/cli": "^2.22.0",
"@commitlint/cli": "^18.0.0",
"@commitlint/config-conventional": "^18.0.0",
Expand Down
11 changes: 11 additions & 0 deletions packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
"./list-item-block": {
"types": "./lib/list-item-block/index.d.ts",
"import": "./lib/list-item-block/index.es.js"
},
"./link-tooltip": {
"types": "./lib/link-tooltip/index.d.ts",
"import": "./lib/link-tooltip/index.es.js"
}
},
"main": "./lib/index.es.js",
Expand All @@ -43,6 +47,9 @@
],
"list-item-block": [
"./lib/list-item-block/index.d.ts"
],
"link-tooltip": [
"./lib/link-tooltip/index.d.ts"
]
}
},
Expand All @@ -62,6 +69,7 @@
"@codemirror/view": "^6",
"@milkdown/core": "^7.2.0",
"@milkdown/ctx": "^7.2.0",
"@milkdown/plugin-tooltip": "^7.2.0",
"@milkdown/preset-commonmark": "^7.2.0",
"@milkdown/prose": "^7.2.0",
"@milkdown/transformer": "^7.2.0"
Expand All @@ -71,8 +79,10 @@
"@floating-ui/dom": "^1.5.1",
"@milkdown/exception": "workspace:*",
"@milkdown/utils": "workspace:*",
"@types/lodash.debounce": "^4.0.7",
"atomico": "^1.74.0",
"clsx": "^2.0.0",
"lodash.debounce": "^4.0.8",
"tslib": "^2.5.0",
"unist-util-visit": "^5.0.0"
},
Expand All @@ -82,6 +92,7 @@
"@codemirror/view": "^6.16.0",
"@milkdown/core": "workspace:*",
"@milkdown/ctx": "workspace:*",
"@milkdown/plugin-tooltip": "workspace:*",
"@milkdown/preset-commonmark": "workspace:*",
"@milkdown/prose": "workspace:*",
"@milkdown/transformer": "workspace:*"
Expand Down
12 changes: 12 additions & 0 deletions packages/components/src/__internal__/icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,15 @@ export const search = html`
<path stroke-linecap="round" stroke-linejoin="round" d="M21 21l-5.197-5.197m0 0A7.5 7.5 0 105.196 5.196a7.5 7.5 0 0010.607 10.607z" />
</svg>
`

export const link = html`
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M13.19 8.688a4.5 4.5 0 011.242 7.244l-4.5 4.5a4.5 4.5 0 01-6.364-6.364l1.757-1.757m13.35-.622l1.757-1.757a4.5 4.5 0 00-6.364-6.364l-4.5 4.5a4.5 4.5 0 001.242 7.244" />
</svg>
`

export const trash = html`
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M14.74 9l-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 01-2.244 2.077H8.084a2.25 2.25 0 01-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 00-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 013.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 00-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 00-7.5 0" />
</svg>
`
4 changes: 2 additions & 2 deletions packages/components/src/image-block/view/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ export const imageComponent: Component<ImageComponentProps> = ({
</span>
</div>
</div>
<button
<div
class=${clsx('confirm', currentLink.length === 0 && 'hidden')}
onclick=${() => onConfirmLinkInput()}
>
${config?.confirmButton()}
</button>
</div>
</div>
<div class=${clsx('image-wrapper', src.length === 0 && 'hidden')}>
<div class="operation">
Expand Down
4 changes: 4 additions & 0 deletions packages/components/src/image-block/view/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ export const style = css`
display: flex;
}
:host > .image-edit .confirm {
cursor: pointer;
}
:host > .image-edit .link-importer {
position: relative;
flex: 1;
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
export * from './image-block'
export * from './list-item-block'
export * from './code-block'
export * from './link-tooltip'
9 changes: 9 additions & 0 deletions packages/components/src/link-tooltip/configure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* Copyright 2021, Milkdown by Mirone. */
import type { Ctx } from '@milkdown/ctx'
import { configureLinkPreviewTooltip } from './preview/preview-configure'
import { configureLinkEditTooltip } from './edit/edit-configure'

export function configureLinkTooltip(ctx: Ctx) {
configureLinkPreviewTooltip(ctx)
configureLinkEditTooltip(ctx)
}
Loading

0 comments on commit 1e8a627

Please sign in to comment.