Skip to content

Commit

Permalink
Added custom Vuetify icons import functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
sronveaux committed Jul 12, 2024
1 parent 41e4892 commit 886bb8d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
// (runtime-only or standalone) has been set in vue.config.js with runtimeCompiler.
import Vue from 'vue';
import Vuetify from 'vuetify/lib/framework';
import PortalVue from 'portal-vue'
import 'roboto-fontface/css/roboto/roboto-fontface.css'
import '@mdi/font/css/materialdesignicons.css'
import 'material-icons/iconfont/material-icons.css'
import '../node_modules/ol/ol.css';
import PortalVue from 'portal-vue';
import 'roboto-fontface/css/roboto/roboto-fontface.css';
import '@mdi/font/css/materialdesignicons.css';
import 'material-icons/iconfont/material-icons.css';
import 'ol/ol.css';
import WguApp from '../app/WguApp';
import UrlUtil from './util/Url';
import IconUtil from './util/Icon';
import LocaleUtil from './util/Locale';
import ObjectUtil from './util/Object';
import ColorThemeUtil from './util/ColorTheme'
import ColorThemeUtil from './util/ColorTheme';
import axios from 'axios';

Vue.use(Vuetify);
Expand Down Expand Up @@ -50,7 +51,8 @@ const createVuetify = function (appConfig) {
const preset = {
theme: ColorThemeUtil.buildTheme(appConfig.colorTheme),
icons: {
iconfont: 'mdiSvg'
iconfont: 'mdiSvg',
values: IconUtil.importIcons()
},
lang: {
current: LocaleUtil.getPreferredLanguage(appConfig),
Expand Down
56 changes: 56 additions & 0 deletions src/util/Icon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import ObjectUtil from './Object.js';

/**
* Icon related utility methods.
*/
const IconUtil = {

/**
* Import custom icons from 'app/icons'. Those icons will be included in an
* object ready to be injected inside Vuetify configuration options object under
* the icons['values'] property.
*
* @returns {Object} An object containing the imported icons.
* Key is the icon name, value contains the icon.
*/
importIcons () {
const icons = {};

try {
const pathIcons = IconUtil.importPathIcons();
ObjectUtil.mergeDeep(icons, pathIcons);
} catch (e) {
}

return icons;
},

/**
* Import custom icons from 'app/icons/*.js'. Those icons will be included in an
* object ready to be injected inside Vuetify configuration options object under
* the icons['values'] property.
* The icons must be .js files with a default export which contains an SVG path.
*
* @returns {Object} An object containing icon SVG paths.
* Key is the icon name, value contains the path.
*/
importPathIcons () {
const moduleDefaultExtractor = (i) => i.default;
const testExp = /icons\/(?:.+\/)*([a-z0-9_-]+).js$/i;

const context = require.context(
'../../app',
true,
/icons\/(?:(?:.+)\/)*([a-z0-9_-]+).js$/i
);
const pathIcons = {};
for (const key of context.keys()) {
const iconName = key.match(testExp)[1];
pathIcons[iconName] = moduleDefaultExtractor(context(key));
}

return pathIcons;
}
}

export default IconUtil;

0 comments on commit 886bb8d

Please sign in to comment.