-
-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add link tooltip component * chore: optimize * chore: auto focus edit input * feat: optimize style
- Loading branch information
1 parent
9f73f77
commit 1e8a627
Showing
25 changed files
with
978 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.