@codemirror/view#WidgetType TypeScript Examples
The following examples show how to use
@codemirror/view#WidgetType.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EmbedDecoration.ts From obsidian-plantuml with MIT License | 6 votes |
class EmojiWidget extends WidgetType {
private readonly source: HTMLDivElement;
constructor(source: HTMLDivElement) {
super();
this.source = source;
}
eq(other: EmojiWidget) {
return other == this;
}
toDOM() {
return this.source;
}
ignoreEvent(): boolean {
return false;
}
}
Example #2
Source File: livePreview.ts From obsidian_supercharged_links with MIT License | 6 votes |
// Implements the live preview supercharging
// Code structure based on https://github.com/nothingislost/obsidian-cm6-attributes/blob/743d71b0aa616407149a0b6ea5ffea28e2154158/src/main.ts
// Code help credits to @NothingIsLost! They have been a great help getting this to work properly.
class HeaderWidget extends WidgetType {
attributes: Record<string, string>
after: boolean
constructor(attributes: Record<string, string>, after: boolean) {
super();
this.attributes = attributes
this.after = after
}
toDOM() {
let headerEl = document.createElement("span");
headerEl.setAttrs(this.attributes);
if (this.after) {
headerEl.addClass('data-link-icon-after');
}
else {
headerEl.addClass('data-link-icon')
}
// create a naive bread crumb
return headerEl;
}
ignoreEvent() {
return true;
}
}
Example #3
Source File: IconWidget.ts From obsidian-banners with MIT License | 6 votes |
export default class IconWidget extends WidgetType {
plugin: BannersPlugin;
icon: string;
file: TFile;
settingsFacet: PartialSettings;
constructor(plugin: BannersPlugin, icon: string, file: TFile, settingsFacet: PartialSettings) {
super();
this.plugin = plugin;
this.icon = icon;
this.file = file;
this.settingsFacet = settingsFacet;
}
eq(widget: IconWidget): boolean {
const { icon, file, settingsFacet } = widget;
return (
this.icon === icon &&
this.file === file &&
this.settingsFacet === settingsFacet
);
}
toDOM(): HTMLElement {
const { iconHorizontalAlignment: ha, iconVerticalAlignment: va } = this.plugin.settings;
const wrap = document.createElement('div');
wrap.addClass('obsidian-banner-icon', 'cm6-banner-icon', `h-${ha}`, `v-${va}`);
const el = buildIcon(this.plugin, this.icon, this.file);
wrap.append(el);
return wrap;
}
}
Example #4
Source File: livePreview.ts From obsidian_supercharged_links with MIT License | 5 votes |
export function buildCMViewPlugin(app: App, _settings: SuperchargedLinksSettings)
{
// Implements the live preview supercharging
// Code structure based on https://github.com/nothingislost/obsidian-cm6-attributes/blob/743d71b0aa616407149a0b6ea5ffea28e2154158/src/main.ts
// Code help credits to @NothingIsLost! They have been a great help getting this to work properly.
class HeaderWidget extends WidgetType {
attributes: Record<string, string>
after: boolean
constructor(attributes: Record<string, string>, after: boolean) {
super();
this.attributes = attributes
this.after = after
}
toDOM() {
let headerEl = document.createElement("span");
headerEl.setAttrs(this.attributes);
if (this.after) {
headerEl.addClass('data-link-icon-after');
}
else {
headerEl.addClass('data-link-icon')
}
// create a naive bread crumb
return headerEl;
}
ignoreEvent() {
return true;
}
}
const settings = _settings;
const viewPlugin = ViewPlugin.fromClass(
class {
decorations: DecorationSet;
constructor(view: EditorView) {
this.decorations = this.buildDecorations(view);
}
update(update: ViewUpdate) {
if (update.docChanged || update.viewportChanged) {
this.decorations = this.buildDecorations(update.view);
}
}
destroy() {
}
buildDecorations(view: EditorView) {
let builder = new RangeSetBuilder<Decoration>();
if (!settings.enableEditor) {
return builder.finish();
}
const mdView = view.state.field(editorViewField) as MarkdownView;
let lastAttributes = {};
let iconDecoAfter: Decoration = null;
let iconDecoAfterWhere: number = null;
let mdAliasFrom: number = null;
let mdAliasTo: number = null;
for (let {from, to} of view.visibleRanges) {
syntaxTree(view.state).iterate({
from,
to,
enter: (type, from, to) => {
const tokenProps = type.prop(tokenClassNodeProp);
if (tokenProps) {
const props = new Set(tokenProps.split(" "));
const isLink = props.has("hmd-internal-link");
const isAlias = props.has("link-alias");
const isPipe = props.has("link-alias-pipe");
// The 'alias' of the md link
const isMDLink = props.has('link');
// The 'internal link' of the md link
const isMDUrl = props.has('url');
const isMDFormatting = props.has('formatting-link');
if (isMDLink && !isMDFormatting) {
// Link: The 'alias'
// URL: The internal link
mdAliasFrom = from;
mdAliasTo = to;
}
if (!isPipe && !isAlias) {
if (iconDecoAfter) {
builder.add(iconDecoAfterWhere, iconDecoAfterWhere, iconDecoAfter);
iconDecoAfter = null;
iconDecoAfterWhere = null;
}
}
if (isLink && !isAlias && !isPipe || isMDUrl) {
let linkText = view.state.doc.sliceString(from, to);
linkText = linkText.split("#")[0];
let file = app.metadataCache.getFirstLinkpathDest(linkText, mdView.file.basename);
if (isMDUrl && !file) {
try {
file = app.vault.getAbstractFileByPath(decodeURIComponent(linkText)) as TFile;
}
catch(e) {}
}
if (file) {
let _attributes = fetchTargetAttributesSync(app, settings, file, true);
let attributes: Record<string, string> = {};
for (let key in _attributes) {
attributes["data-link-" + key] = _attributes[key];
}
let deco = Decoration.mark({
attributes,
class: "data-link-text"
});
let iconDecoBefore = Decoration.widget({
widget: new HeaderWidget(attributes, false),
});
iconDecoAfter = Decoration.widget({
widget: new HeaderWidget(attributes, true),
});
if (isMDUrl) {
// Apply retroactively to the alias found before
let deco = Decoration.mark({
attributes: attributes,
class: "data-link-text"
});
builder.add(mdAliasFrom, mdAliasFrom, iconDecoBefore);
builder.add(mdAliasFrom, mdAliasTo, deco);
if (iconDecoAfter) {
builder.add(mdAliasTo, mdAliasTo, iconDecoAfter);
iconDecoAfter = null;
iconDecoAfterWhere = null;
mdAliasFrom = null;
mdAliasTo = null;
}
}
else {
builder.add(from, from, iconDecoBefore);
}
builder.add(from, to, deco);
lastAttributes = attributes;
iconDecoAfterWhere = to;
}
} else if (isLink && isAlias) {
let deco = Decoration.mark({
attributes: lastAttributes,
class: "data-link-text"
});
builder.add(from, to, deco);
if (iconDecoAfter) {
builder.add(to, to, iconDecoAfter);
iconDecoAfter = null;
iconDecoAfterWhere = null;
}
}
}
}
})
}
return builder.finish();
}
},
{
decorations: v => v.decorations
}
);
return viewPlugin;
}
Example #5
Source File: BannerWidget.ts From obsidian-banners with MIT License | 5 votes |
export default class BannerWidget extends WidgetType {
plugin: BannersPlugin;
bannerData: IBannerMetadata;
filepath: string;
contentEl: HTMLElement;
settingsFacet: PartialSettings;
removeListeners: () => void;
constructor(plugin: BannersPlugin, bannerData: IBannerMetadata, filepath: string, contentEl: HTMLElement, settingsFacet: PartialSettings) {
super();
this.plugin = plugin;
this.bannerData = bannerData;
this.filepath = filepath;
this.contentEl = contentEl;
this.settingsFacet = settingsFacet;
this.removeListeners = () => {};
}
eq(widget: BannerWidget): boolean {
const { bannerData: { src, x, y, lock }, filepath, settingsFacet } = widget;
return (
this.bannerData.src === src &&
this.bannerData.x === x &&
this.bannerData.y === y &&
this.bannerData.lock === lock &&
this.filepath === filepath &&
this.settingsFacet === settingsFacet
);
}
toDOM(): HTMLElement {
const { plugin, bannerData, filepath, contentEl } = this;
const wrap = document.createElement('div');
wrap.addClass('obsidian-banner', 'cm6-banner', plugin.settings.style);
const [elements, removeListeners] = buildBanner(plugin, bannerData, filepath, wrap, contentEl);
wrap.append(...elements);
this.removeListeners = removeListeners;
return wrap;
}
destroy() {
this.removeListeners();
}
}
Example #6
Source File: SpacerWidget.ts From obsidian-banners with MIT License | 5 votes |
export default class SpacerWidget extends WidgetType {
toDOM(): HTMLElement {
const spacer = document.createElement('div');
spacer.addClass('obsidian-banner-spacer');
return spacer;
}
}