Skip to content
This repository has been archived by the owner on Aug 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2 from Dolphiq/development
Browse files Browse the repository at this point in the history
Small fixes and improvements
  • Loading branch information
johanzandstra authored Oct 6, 2017
2 parents 418e558 + 99130d5 commit 6be2d73
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 61 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
# Changelog

## 1.0.3 - 2017-10-06

### Changed
- Removed dynamic resource file from the repository

### Added
- Added screenshots to the readme.
- Added resource links to download font packages

## 1.0.2 - 2017-09-06
- Initial release
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
Craft plugin that provides a new field type that offers users an easy way to pick an icon from a .woff or .ttf font file.
You can easily use ionicons or font awesome icons

**Note**: This plugin may become a paid add-on when the Craft Plugin store becomes available.

## Requirements
* Craft 3.0 (beta 20)+
* PHP 7.0+
* PhenX PHP Font Lib
* One or more .ttf or .woff files

## Example

### Example of the field usage in the template for a cms user
Expand Down Expand Up @@ -80,5 +88,6 @@ Display an icon with a custom class:


### Contributors & Developers
Lucas Weijers - [email protected]
Brought to you by [Dolphiq](Https://dolphiq.nl)
Lucas Weijers - Original developer
Johan Zandstra - Minor changes
Brought to you by [Dolphiq](Https://dolphiq.nl) [email protected]
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "dolphiq/iconpicker",
"description": "Iconpicker",
"version": "1.0.2",
"description": "Craft Iconpicker plugin provides a new field type that offers users an easy way to pick an icon from a .woff or .ttf font file.",
"version": "1.0.3",
"keywords": [
"craft",
"cms",
Expand All @@ -14,7 +14,7 @@
"type": "craft-plugin",
"license": "MIT",
"require": {
"craftcms/cms": "^3.0.0-beta.10",
"craftcms/cms": "^3.0.0-beta.20",
"phenx/php-font-lib": "^0.5.0"
},
"autoload": {
Expand All @@ -32,6 +32,7 @@
"extra": {
"handle": "dolphiq-iconpicker",
"name": "Iconpicker",
"description": "Craft Iconpicker plugin provides a new field type that offers users an easy way to pick an icon from a .woff or .ttf font file.",
"schemaVersion": "1.0.0",
"developer": "Dolphiq",
"developerUrl": "https://dolphiq.nl/",
Expand Down
8 changes: 2 additions & 6 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,13 @@

class Plugin extends \craft\base\Plugin
{

public function init()
{
parent::init();

// Register field type
Event::on(Fields::class, Fields::EVENT_REGISTER_FIELD_TYPES, function(RegisterComponentTypesEvent $event) {
$event->types[] = Iconpicker::class;
Event::on(Fields::class, Fields::EVENT_REGISTER_FIELD_TYPES, function (RegisterComponentTypesEvent $event) {
$event->types[] = Iconpicker::class;
});
}

}

?>
2 changes: 1 addition & 1 deletion src/assets/appAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ public function init()

parent::init();
}
}
}
2 changes: 1 addition & 1 deletion src/assets/sharedAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ public function init()

parent::init();
}
}
}
57 changes: 27 additions & 30 deletions src/fields/Iconpicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use yii\db\Schema;
use yii\helpers\ArrayHelper;


class Iconpicker extends Field implements PreviewableFieldInterface
{
// Static
Expand All @@ -29,22 +28,22 @@ class Iconpicker extends Field implements PreviewableFieldInterface
/**
* @var string The directory where the fonts are
*/
CONST FONT_DIR = '@vendor/dolphiq/iconpicker/src/resources-shared/fonts/';
const FONT_DIR = '@vendor/dolphiq/iconpicker/src/resources-shared/fonts/';

/**
* @var array All extentions that are allowed to be imported as a font
*/
CONST FONT_EXT = ['*.woff', '*.ttf'];
const FONT_EXT = ['*.woff', '*.ttf'];

/**
* @var string Icon class
*/
CONST ICON_CLASS = 'dq-icon-';
const ICON_CLASS = 'dq-icon-';

/**
* @var string Pattern to format a safe file name
*/
CONST SAFE_NAME_PATTERN = '/[^A-Za-z0-9_\-]/';
const SAFE_NAME_PATTERN = '/[^A-Za-z0-9_\-]/';

/**
* @inheritdoc
Expand Down Expand Up @@ -167,13 +166,14 @@ public function normalizeValue($value, ElementInterface $element = null)
* @return array
*/

private function getFonts(){
if(empty($this->fonts)) {
private function getFonts()
{
if (empty($this->fonts)) {
$files = FileHelper::findFiles(Craft::getAlias(self::FONT_DIR), ['only' => self::FONT_EXT]);
$filenames = [];
$fonts = [];

foreach ($files as $file){
foreach ($files as $file) {
$pathInfo = pathinfo($file);
$safename = in_array($pathInfo['filename'], $filenames) ? $pathInfo['basename'] : $pathInfo['filename'];
$safename = $this->safeName($safename);
Expand All @@ -198,9 +198,10 @@ private function getFonts(){
* Returns a options list for the settings dropdown when defining a field
* @return array
*/
private function getFontOptions(){
private function getFontOptions()
{
$f = $this->getFonts();
if(!empty($f)){
if (!empty($f)) {
return ArrayHelper::map($f, 'safename', 'basename');
}
}
Expand All @@ -210,11 +211,11 @@ private function getFontOptions(){
*
* @return array|null
*/
private function getIcons(){

if(!empty($this->iconFont)) {
private function getIcons()
{
if (!empty($this->iconFont)) {
$fonts = $this->getFonts();
if(!empty($fonts) && isset($fonts[$this->iconFont])) {
if (!empty($fonts) && isset($fonts[$this->iconFont])) {
$font = Font::load($fonts[$this->iconFont]['path']);
$font->parse();

Expand All @@ -231,10 +232,11 @@ private function getIcons(){
* @param $font \FontLib\TrueType\File|null
* @return array|null
*/
private function getUnicodeList($font){
if(!empty($font)){
private function getUnicodeList($font)
{
if (!empty($font)) {
$unicodes = $font->getUnicodeCharMap();
if(!empty($unicodes)){
if (!empty($unicodes)) {
return array_keys($unicodes);
}
}
Expand All @@ -246,21 +248,20 @@ private function getUnicodeList($font){
/**
* Generate a css file that creates font families for each font file in the font directory
*/
public function getFontCss(){

public function getFontCss()
{
$sharedAsset = new sharedAsset();
$scss = "";

foreach ($this->getFonts() as $safeName => $pathInfo){
foreach ($this->getFonts() as $safeName => $pathInfo) {
$fontFile = $pathInfo['path'];
$font = Font::load($fontFile);
$font->parse();

if(!empty($font)) {
if (!empty($font)) {
$iconFontName = $safeName;

if (!empty($iconFontName)) {

$scss .= "
@font-face {
font-family: 'dq-iconpicker-".$iconFontName."';
Expand Down Expand Up @@ -288,9 +289,6 @@ public function getFontCss(){
-moz-osx-font-smoothing: grayscale;
}'."\n\n";



}
}
}
Expand All @@ -299,17 +297,16 @@ public function getFontCss(){

// Register the assetbundle that loads the generated css
Craft::$app->view->registerAssetBundle(sharedAsset::className());

}

// Get the fontname of the currently selected font.
public function getIconFontName(){

public function getIconFontName()
{
return $this->iconFont;

}

private function safeName($filename){
private function safeName($filename)
{
$name = preg_replace(self::SAFE_NAME_PATTERN, '-', $filename);
return $name;
}
Expand Down
24 changes: 13 additions & 11 deletions src/models/IconpickerModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@
use Craft;
use craft\base\Model;



class IconpickerModel extends Model{

class IconpickerModel extends Model
{
public $icon;
public $iconFont;

Expand All @@ -34,24 +32,28 @@ public function rules()
];
}

public function getIconChar(){
public function getIconChar()
{
return '&#'.$this->icon.';';
}

public function getIconCharHex(){
public function getIconCharHex()
{
return '&#x'.$this->getIconHex().';';
}

public function getIconHex(){
public function getIconHex()
{
return dechex($this->icon);
}

public function getIconSpan(){
public function getIconSpan()
{
return '<span class="'.$this->getIconClass().'">'.$this->getIconCharHex().'</span>';
}

public function getIconClass(){
public function getIconClass()
{
return 'dq-icon-'.$this->iconFont;
}

}
}
7 changes: 6 additions & 1 deletion src/templates/fieldSettings.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

{% else %}

<p>You need to put at least one font in the plugins font directory. (src/resources-shared/fonts)</p>
<p class="warning">You need to put at least one font (.ttf or .woff) in the plugins font directory. (src/resources-shared/fonts)</p>

<h2>Font resources:</h2>
<ul>
<li><a href="http://ionicons.com/" target="_blank">ionicons</a>
<li><a href="http://fontawesome.io/" target="_blank">fontawesome</a>
</ul>
{% endif %}
13 changes: 7 additions & 6 deletions src/views/main/_field.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@

$field->getFontCss();

if(count($icons) > 0) {

if (count($icons) > 0) {
?>


Expand Down Expand Up @@ -45,8 +44,7 @@
}
} else {
echo 'No icons found in the selected font';
}
?>
} ?>
</div>
</div>
</div>
Expand All @@ -61,6 +59,9 @@
<div class="resizehandle"></div>
</div>

<?php } else { ?>
<?php
} else {
?>
<p>There is no font uploaded to the font folder of this plugin, no font selected in the field settings, or the font contains no icons</p>
<?php } ?>
<?php
} ?>

0 comments on commit 6be2d73

Please sign in to comment.