Skip to content
This repository was archived by the owner on Jun 14, 2023. It is now read-only.

Commit ff1e726

Browse files
author
Jayden Smith
committed
Initial commit.
0 parents  commit ff1e726

File tree

16 files changed

+720
-0
lines changed

16 files changed

+720
-0
lines changed

.craftplugin

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"pluginName":"GraphQL Srcset","pluginDescription":"Adds the @srcset GraphQL directive for generating a comma-separated list of image transforms.","pluginVersion":"1.0.0","pluginAuthorName":"Jayden Smith","pluginVendorName":"tasdev-au","pluginAuthorUrl":"https://tas.dev","pluginAuthorGithub":"tasdev-au","codeComments":"","pluginComponents":["settings"],"consolecommandName":"","controllerName":"","cpsectionName":"","elementName":"","fieldName":"","modelName":"","purchasableName":"","recordName":"","serviceName":"","taskName":"","utilityName":"","widgetName":"","apiVersion":"api_version_3_0"}

.gitignore

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# CRAFT ENVIRONMENT
2+
.env.php
3+
.env.sh
4+
.env
5+
6+
# COMPOSER
7+
/vendor
8+
9+
# BUILD FILES
10+
/bower_components/*
11+
/node_modules/*
12+
/build/*
13+
/yarn-error.log
14+
15+
# MISC FILES
16+
.cache
17+
.DS_Store
18+
.idea
19+
.project
20+
.settings
21+
*.esproj
22+
*.sublime-workspace
23+
*.sublime-project
24+
*.tmproj
25+
*.tmproject
26+
.vscode/*
27+
!.vscode/settings.json
28+
!.vscode/tasks.json
29+
!.vscode/launch.json
30+
!.vscode/extensions.json
31+
config.codekit3
32+
prepros-6.config

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# GraphQL Srcset Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).
6+
7+
## 1.0.0 - 2020-03-29
8+
### Added
9+
- Initial release

LICENSE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2020 Jayden Smith
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# GraphQL Srcset Plugin for Craft CMS
2+
3+
Adds the @srcset GraphQL directive for generating a comma-separated list of image transforms.
4+
5+
<img width="500" src="https://tas.dev/uploads/plugins/graphql-srcset/screenshot-1.png" style="box-shadow: 0 4px 16px rgba(0,0,0,0.08); border-radius: 4px; border: 1px solid rgba(0,0,0,0.12);">
6+
7+
## Features
8+
9+
- Set default widths for all srcset transforms.
10+
- Specify a ratio to generate all images using the same aspect ratio.
11+
12+
## Toubleshooting
13+
- If you're having issues with the directive not simply returning the URL with no transforms, you must specify at least one argument in the directive. Consider adding the `immediately` argument.
14+
15+
## Support
16+
17+
[Create a Github issue](https://github.com/tasdev-au/craft-graphql-srcset/issues) if you experience a bug with the GraphQL Srcset plugin.
18+
19+
<a href="https://tas.dev" target="_blank">
20+
<img width="100" src="https://tas.dev/assets/img/logo-text.svg">
21+
</a>

composer.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name": "tasdev-au/craft-graphql-srcset",
3+
"description": "Adds the @srcset GraphQL directive for generating a comma-separated list of image transforms.",
4+
"type": "craft-plugin",
5+
"version": "1.0.0",
6+
"keywords": [
7+
"craft",
8+
"cms",
9+
"craftcms",
10+
"craft-plugin",
11+
"graphql srcset"
12+
],
13+
"support": {
14+
"docs": "https://github.com/tasdev-au/graph-ql-srcset/blob/master/README.md",
15+
"issues": "https://github.com/tasdev-au/craft-graphql-srcset/issues"
16+
},
17+
"license": "MIT",
18+
"authors": [
19+
{
20+
"name": "Jayden Smith",
21+
"homepage": "https://tas.dev"
22+
}
23+
],
24+
"require": {
25+
"craftcms/cms": "^3.0.0-RC1"
26+
},
27+
"autoload": {
28+
"psr-4": {
29+
"tasdev\\graphqlsrcset\\": "src/"
30+
}
31+
},
32+
"extra": {
33+
"name": "GraphQL Srcset",
34+
"handle": "graphql-srcset",
35+
"developer": "Jayden Smith",
36+
"developerUrl": "https://tas.dev",
37+
"documentationUrl": "https://github.com/tasdev-au/craft-graphql-srcset/blob/master/README.md",
38+
"changelogUrl": "https://raw.githubusercontent.com/tasdev-au/craft-graphql-srcset/master/CHANGELOG.md",
39+
"class": "tasdev\\graphqlsrcset\\GraphqlSrcset"
40+
}
41+
}

src/GraphqlSrcset.php

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
/**
3+
* GraphQL Srcset plugin for Craft CMS 3.x
4+
*
5+
* Adds the @srcset GraphQL directive for generating a comma-separated list of image transforms.
6+
*
7+
* @link https://tas.dev
8+
* @copyright Copyright (c) 2020 Jayden Smith
9+
*/
10+
11+
namespace tasdev\graphqlsrcset;
12+
13+
use Craft;
14+
use craft\base\Plugin;
15+
use craft\events\RegisterGqlDirectivesEvent;
16+
use craft\services\Gql;
17+
use craft\services\Plugins;
18+
use craft\events\PluginEvent;
19+
use craft\helpers\UrlHelper;
20+
use craft\web\twig\variables\CraftVariable;
21+
22+
use yii\base\Event;
23+
24+
use tasdev\graphqlsrcset\models\Settings;
25+
use tasdev\graphqlsrcset\gql\directives\SrcSet;
26+
use tasdev\graphqlsrcset\variables\GraphqlSrcset as GraphqlSrcsetVariable;
27+
28+
/**
29+
* Class GraphqlSrcset
30+
*
31+
* @author Jayden Smith
32+
* @package GraphqlSrcset
33+
* @since 1.0.0
34+
*
35+
*/
36+
class GraphqlSrcset extends Plugin
37+
{
38+
// Static Properties
39+
// =========================================================================
40+
41+
/**
42+
* @var GraphqlSrcset
43+
*/
44+
public static $plugin;
45+
46+
47+
// Public Properties
48+
// =========================================================================
49+
50+
/**
51+
* @var string
52+
*/
53+
public $schemaVersion = '1.0.0';
54+
55+
/**
56+
* @var bool
57+
*/
58+
public $hasCpSettings = true;
59+
60+
/**
61+
* @var bool
62+
*/
63+
public $hasCpSection = false;
64+
65+
66+
// Public Methods
67+
// =========================================================================
68+
69+
/**
70+
* @inheritdoc
71+
*/
72+
public function init()
73+
{
74+
parent::init();
75+
76+
$this->_registerEventHandlers();
77+
$this->_registerVariable();
78+
}
79+
80+
/**
81+
* @inheritdoc
82+
*/
83+
public function getSettingsResponse()
84+
{
85+
return Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('graphql-srcset/settings'));
86+
}
87+
88+
89+
// Protected Methods
90+
// =========================================================================
91+
92+
/**
93+
* @inheritdoc
94+
*/
95+
protected function createSettingsModel()
96+
{
97+
return new Settings();
98+
}
99+
100+
101+
// Private Methods
102+
// =========================================================================
103+
104+
/**
105+
* Register the event handlers.
106+
*/
107+
private function _registerEventHandlers()
108+
{
109+
Event::on(Gql::class, Gql::EVENT_REGISTER_GQL_DIRECTIVES, function (RegisterGqlDirectivesEvent $event) {
110+
$event->directives[] = SrcSet::class;
111+
});
112+
113+
// Redirect after plugin install
114+
Event::on(Plugins::class, Plugins::EVENT_AFTER_INSTALL_PLUGIN, function (PluginEvent $event) {
115+
if ($event->plugin === $this) {
116+
Craft::$app->getGql()->flushCaches();
117+
118+
if (Craft::$app->getRequest()->isCpRequest) {
119+
Craft::$app->getResponse()->redirect(
120+
UrlHelper::cpUrl('graphql-srcset/settings')
121+
)->send();
122+
}
123+
}
124+
});
125+
}
126+
127+
/**
128+
* Register Auctions template variable
129+
*/
130+
private function _registerVariable()
131+
{
132+
Event::on(CraftVariable::class, CraftVariable::EVENT_INIT, function(Event $event) {
133+
/** @var CraftVariable $variable */
134+
$variable = $event->sender;
135+
$variable->set('graphqlSrcset', GraphqlSrcsetVariable::class);
136+
});
137+
}
138+
}

src/config.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* GraphQL Srcset plugin for Craft CMS 3.x
4+
*
5+
* Adds the @srcset GraphQL directive for generating a comma-separated list of image transforms.
6+
*
7+
* @link https://tas.dev
8+
* @copyright Copyright (c) 2020 Jayden Smith
9+
*/
10+
11+
/**
12+
* GraphQL Srcset config.php
13+
*
14+
* This file exists only as a template for the GraphQL Srcset settings.
15+
* It does nothing on its own.
16+
*
17+
* Don't edit this file, instead copy it to 'craft/config' as 'graphql-srcset.php'
18+
* and make your changes there to override default settings.
19+
*
20+
* Once copied to 'craft/config', this file will be multi-environment aware as
21+
* well, so you can have different settings groups for each environment, just as
22+
* you do for 'general.php'
23+
*/
24+
25+
return [
26+
27+
'widths' => [ 640, 768, 1024, 1366, 1600, 1920, 2560 ],
28+
29+
];
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
* GraphQL Srcset plugin for Craft CMS 3.x
4+
*
5+
* Adds the @srcset GraphQL directive for generating a comma-separated list of image transforms.
6+
*
7+
* @link https://tas.dev
8+
* @copyright Copyright (c) 2020 Jayden Smith
9+
*/
10+
11+
namespace tasdev\graphqlsrcset\controllers;
12+
13+
use Craft;
14+
use craft\errors\MissingComponentException;
15+
use craft\web\Controller as BaseController;
16+
17+
use yii\web\BadRequestHttpException;
18+
use yii\web\Response;
19+
20+
use tasdev\graphqlsrcset\GraphqlSrcset;
21+
use tasdev\graphqlsrcset\models\Settings as SettingsModel;
22+
23+
/**
24+
* Class SettingsController
25+
*
26+
* @author Jayden Smith
27+
* @package GraphqlSrcset
28+
* @since 1.0.0
29+
*/
30+
class SettingsController extends BaseController
31+
{
32+
// Public Methods
33+
// =========================================================================
34+
35+
/**
36+
* @return Response
37+
* @throws MissingComponentException
38+
* @throws BadRequestHttpException
39+
*/
40+
public function actionSaveSettings(): Response
41+
{
42+
$this->requirePostRequest();
43+
$postData = Craft::$app->getRequest()->getParam('settings');
44+
45+
$widths = [];
46+
foreach ($postData['widths'] as $width) {
47+
$widths[] = $width['width'];
48+
}
49+
50+
sort($widths);
51+
52+
$postData = ['widths' => $widths] + $postData;
53+
$settings = new SettingsModel($postData);
54+
55+
if (!$settings->validate()) {
56+
Craft::$app->getSession()->setError(Craft::t('graphql-srcset', 'Couldn’t save settings.'));
57+
58+
return $this->renderTemplate('graphql-srcset/settings', ['settings' => $settings]);
59+
}
60+
61+
Craft::$app->getPlugins()->savePluginSettings(GraphqlSrcset::getInstance(), $settings->toArray());
62+
63+
Craft::$app->getSession()->setNotice(Craft::t('graphql-srcset', 'Settings saved.'));
64+
65+
return $this->redirectToPostedUrl();
66+
}
67+
}

0 commit comments

Comments
 (0)