Skip to content

Commit

Permalink
refactor: move icons suggestion into editor directory
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianWoelki committed Oct 20, 2023
1 parent 158333a commit 71f6609
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
22 changes: 12 additions & 10 deletions src/iconsSuggestion.ts → src/editor/icons-suggestion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {
EditorSuggestContext,
EditorSuggestTriggerInfo,
} from 'obsidian';
import { getAllLoadedIconNames } from './iconPackManager';
import icon from './lib/icon';
import emoji from './emoji';
import { getAllLoadedIconNames } from '../iconPackManager';
import icon from '../lib/icon';
import emoji from '../emoji';

export default class SuggestionIcon extends EditorSuggest<string> {
constructor(app: App) {
Expand All @@ -22,15 +22,16 @@ export default class SuggestionIcon extends EditorSuggest<string> {
.substring(0, cursor.ch)
.lastIndexOf(':');

// onTrigger needs to return null as soon as possible to save processing performance.
// `onTrigger` needs to return `null` as soon as possible to save processing performance.
if (shortcodeStart == -1) return null;

// Regex for checking if the shortcode is not done yet.
const regexOngoingShortcode = editor
.getLine(cursor.line)
.substring(shortcodeStart, cursor.ch)
.match(/^(:)\w+$/g);

if (regexOngoingShortcode == null) return null;
if (regexOngoingShortcode === null) return null;

const startingIndex = editor
.getLine(cursor.line)
Expand All @@ -52,14 +53,15 @@ export default class SuggestionIcon extends EditorSuggest<string> {
getSuggestions(context: EditorSuggestContext): string[] | Promise<string[]> {
const queryLowerCase = context.query.substring(1).toLowerCase();

// Store all icons corresponding to the current query
// Store all icons corresponding to the current query.
const iconsNameArray = getAllLoadedIconNames()
.filter((iconObject) =>
iconObject.name.toLowerCase().includes(queryLowerCase),
)
.map((iconObject) => iconObject.prefix + iconObject.name);

// Store all emojis correspoding to the current query - parsing whitespaces and colons for shortcodes compatibility
// Store all emojis correspoding to the current query - parsing whitespaces and
// colons for shortcodes compatibility.
const emojisNameArray = Object.keys(emoji.shortNames).filter((e) =>
emoji.getShortcode(e).contains(queryLowerCase),
);
Expand All @@ -70,16 +72,16 @@ export default class SuggestionIcon extends EditorSuggest<string> {
renderSuggestion(value: string, el: HTMLElement): void {
const iconObject = icon.getIconByName(value);
if (iconObject) {
// Suggest an icon
// Suggest an icon.
el.innerHTML = `${iconObject.svgElement} ${value}`;
} else {
// Suggest an emoji - display its shortcode version
// Suggest an emoji - display its shortcode version.
el.innerHTML = `${value} ${emoji.getShortcode(value)}`;
}
}

selectSuggestion(value: string): void {
// Replace query with iconNameWithPrefix or emoji unicode directly
// Replace query with iconNameWithPrefix or emoji unicode directly.
const updatedValue = emoji.isEmoji(value.replace(/_/g, ' '))
? value
: `:${value}:`;
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import {
} from '@app/util';
import config from '@app/config';
import titleIcon from './lib/icon-title';
import SuggestionIcon from './iconsSuggestion';
import SuggestionIcon from './editor/icons-suggestion';
import emoji from './emoji';

export interface FolderIconObject {
Expand Down Expand Up @@ -396,7 +396,7 @@ export default class IconFolderPlugin extends Plugin {
}
});

// shortcodes auto-completion suggestion in notes
// Register shortcodes auto-completion suggestion in notes.
this.registerEditorSuggest(new SuggestionIcon(this.app));

this.addSettingTab(new IconFolderSettingsUI(this.app, this));
Expand Down

0 comments on commit 71f6609

Please sign in to comment.