0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-06-22 04:58:40 +00:00

move to use html img element

This commit is contained in:
Víctor Losada Hernández
2026-05-13 12:45:46 +02:00
parent c66310fbe7
commit 097b941580
3 changed files with 48 additions and 32 deletions
+12 -28
View File
@@ -161,44 +161,28 @@
outline : 1px inset #00000055 !important; outline : 1px inset #00000055 !important;
} }
.cm-image[style] { .cm-image {
position: relative; position:relative;
&::before, &::after { .cm-preview {
content:""; object-fit: contain;
position:absolute; position:absolute;
bottom:0; bottom:0;
left:0; left:0;
translate:0 100%; height:200px;
display:block;
z-index:1000;
pointer-events: none;
opacity:0;
transition:0.2s opacity 0.5s;
}
&::before{
width:200px; width:200px;
height:200px; height:200px;
background-color: #fff; background-color: #fff;
border-radius:10px; border-radius:10px;
border:3px solid grey; border:3px solid grey;
pointer-events: none;
opacity:0;
transition:0.2s opacity 0.5s;
translate:0 100%;
z-index:1000;
} }
&::after { &:hover .cm-preview {
width:190px;
height:190px;
//padding of 5px + 3px border: 8px
translate:8px calc(100% + 8px);
background-image: var(--preview-img);
background-size:contain;
background-repeat:no-repeat;
background-position:center;
filter:drop-shadow(0 0 5px #0008);
}
&:hover::before,
&:hover::after {
opacity:1; opacity:1;
} }
} }
@@ -333,6 +333,33 @@ function getUrl(node, doc) {
return url; return url;
} }
import { WidgetType } from '@codemirror/view';
class ImageWidget extends WidgetType {
constructor(url) {
super();
this.url = url;
}
toDOM() {
const img = document.createElement('img');
img.loading = "lazy";
img.className = 'cm-preview';
img.src = this.url;
img.onerror = ()=>{
img.src = 'client/icons/Broken-image-icon-in-Chrome.png';
};
return img;
}
eq(other) {
return other.url === this.url;
}
}
export function customHighlightPlugin(renderer, tab) { export function customHighlightPlugin(renderer, tab) {
//this function takes the custom tokens created in the tokenize function in customhighlight files //this function takes the custom tokens created in the tokenize function in customhighlight files
//takes the tokens defined by that function and assigns classes to them //takes the tokens defined by that function and assigns classes to them
@@ -368,16 +395,21 @@ export function customHighlightPlugin(renderer, tab) {
if(node.name === 'Image') { if(node.name === 'Image') {
const url = getUrl(node, view.state.doc); const url = getUrl(node, view.state.doc);
console.log(node.node.lastChild.from);
if(!url) return; if(!url) return;
decos.push( decos.push(
Decoration.mark({ Decoration.mark({
class : 'cm-image', class : 'cm-image'
attributes : {
'style' : `--preview-img:url(${url});`
}
}).range(node.from, node.to) }).range(node.from, node.to)
); );
decos.push(
Decoration.widget({
widget : new ImageWidget(url),
side : 1
}).range(node.node.lastChild.from)
);
} }
} }
}); });
Binary file not shown.

After

Width:  |  Height:  |  Size: 9.0 KiB