Skip to content

Commit

Permalink
Merge pull request #27 from BeAPI/feature/multi-sprite-svg
Browse files Browse the repository at this point in the history
Feature/multi sprite svg
  • Loading branch information
firestar300 authored Nov 29, 2022
2 parents d84c667 + b2e5191 commit af097c8
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 21 deletions.
2 changes: 1 addition & 1 deletion assets/css/style.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 10 additions & 6 deletions assets/js/input-5.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
if (!css.id) {
return css.text;
}
var id = css.id;

if (css.url) {
fetchSvg(css.url, css.id, css.text);
return $('<span class="acf_svg__icon icon" data-id="' + css.id + '">' + css.text + '</span>');
fetchSvg(css.url, id, css.text);
return $('<span class="acf_svg__span icon" data-id="' + id + '">' + css.text + '</span>');
} else {
return $('<svg class="acf_svg__icon icon ' + css.id + '" aria-hidden="true" role="img"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#' + css.id + '"></use></svg>' + css.text );
return $('<svg class="acf_svg__icon icon ' + id + '" aria-hidden="true" role="img"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#' + id.substring(id.indexOf('#') + 1) + '"></use></svg>' + css.text);
}
};

Expand All @@ -47,11 +49,13 @@
if (!css.id) {
return css.text;
}
var id = css.id;

if (css.url) {
fetchSvg(css.url, css.id, css.text);
return $('<span class="acf_svg__icon icon" data-id="' + css.id + '">' + css.text + '</span>');
fetchSvg(css.url, id, css.text);
return $('<span class="acf_svg__icon icon" data-id="' + id + '">' + css.text + '</span>');
} else {
return $('<svg class="acf_svg__icon icon ' + css.id + '" aria-hidden="true" role="img"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#' + css.id + '"></use></svg>' + css.text );
return $('<svg class="acf_svg__icon icon ' + id + '" aria-hidden="true" role="img"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#' + id.substring(id.indexOf('#') + 1) + '"></use></svg>' + css.text );
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion assets/js/input-5.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 12 additions & 6 deletions assets/js/input-56.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@
if (!css.id) {
return css.text;
}

var id = css.id;

if (css.url) {
fetchSvg(css.url, css.id, css.text);
return $('<span class="acf_svg__span" data-id="' + css.id + '">' + css.text + '</span>');
fetchSvg(css.url, id, css.text);
return $('<span class="acf_svg__span" data-id="' + id + '">' + css.text + '</span>');
} else {
return $('<span class="acf_svg__span"><svg class="acf_svg__icon icon ' + css.id + '" aria-hidden="true" role="img"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#' + css.id + '"></use></svg>' + css.text + '</span>');
return $('<span class="acf_svg__span"><svg class="acf_svg__icon icon ' + id + '" aria-hidden="true" role="img"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#' + id.substring(id.indexOf('#') + 1) + '"></use></svg>' + css.text + '</span>');
}
};

Expand All @@ -47,11 +50,14 @@
if (!css.id) {
return css.text;
}

var id = css.id;

if (css.url) {
fetchSvg(css.url, css.id, css.text);
return $('<span class="acf_svg__span" data-id="' + css.id + '">' + css.text + '</span>');
fetchSvg(css.url, id, css.text);
return $('<span class="acf_svg__span" data-id="' + id + '">' + css.text + '</span>');
} else {
return $('<span class="acf_svg__span"><svg class="acf_svg__icon small icon ' + css.id + '" aria-hidden="true" role="img"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#' + css.id + '"></use></svg>' + css.text + '</span>');
return $('<span class="acf_svg__span"><svg class="acf_svg__icon small icon ' + id + '" aria-hidden="true" role="img"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#' + id.substring(id.indexOf('#') + 1) + '"></use></svg>' + css.text + '</span>');
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion assets/js/input-56.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 13 additions & 2 deletions fields/acf-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ public function parse_svg() {
$allowed_tags = apply_filters( 'acf_svg_icon_svg_parse_tags', '<symbol><g>' );

$out = array();

// Ignore SVG with type media to check if there are multiple sprite
$custom_files = array_filter(
$files,
function ( $file ) {
return 'media' !== $file['type'];
}
);

foreach ( $files as $file ) {
if ( ! is_file( $file['file'] ) ) {
continue;
Expand All @@ -169,9 +178,11 @@ public function parse_svg() {
preg_match_all( '/id="(\S+)"/m', strip_tags( $contents, $allowed_tags ), $svg );

foreach ( $svg[1] as $id ) {
$id = sanitize_title( $id );
$id = sanitize_title( $id );
// If multiple sprites registered, return sprite name and icon name, otherwise return icon name only
$value = 1 < count( $custom_files ) ? basename( $file['file'] ) . '#' . $id : $id;
$out[] = array(
'id' => $id,
'id' => $value,
'text' => self::get_nice_display_text( $id ),
'disabled' => false,
);
Expand Down
7 changes: 4 additions & 3 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
/*Load all plugin define in package.json*/
var gulp = require('gulp'),
gulpLoadPlugins = require('gulp-load-plugins'),
plugins = gulpLoadPlugins();
plugins = gulpLoadPlugins(),
uglify = require('gulp-uglify-es').default;

/*JS task*/
gulp.task('dist', function () {
gulp.src([ 'assets/js/input-5.js' ])
.pipe(plugins.jshint())
.pipe(plugins.uglify())
.pipe(uglify())
.pipe(plugins.concat('input-5.min.js'))
.pipe(gulp.dest('assets/js/'));

gulp.src([ 'assets/js/input-56.js' ])
.pipe(plugins.jshint())
.pipe(plugins.uglify())
.pipe(uglify())
.pipe(plugins.concat('input-56.min.js'))
.pipe(gulp.dest('assets/js/'));

Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
"gulp-concat": "^2.6.1",
"gulp-jshint": "^2.0.4",
"gulp-load-plugins": "^1.5.0",
"gulp-uglify": "^2.0.1",
"gulp-uglify-es": "^3.0.0",
"gulp-uglifycss": "^1.0.6",
"gulp-watch": "^4.3.11",
"jshint": "^2.9.4"
},
"volta": {
"node": "10.24.1"
}
}

0 comments on commit af097c8

Please sign in to comment.