Skip to content

Commit

Permalink
Refactor thumbnails
Browse files Browse the repository at this point in the history
  • Loading branch information
leoafarias committed Apr 20, 2024
1 parent d2abb0a commit 09c7bf8
Show file tree
Hide file tree
Showing 14 changed files with 480 additions and 303 deletions.
18 changes: 0 additions & 18 deletions example/assets/assets.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion example/assets/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"duration": 0,
"delay": 0,
"curve": "ease"
}
},
"cache_remote_assets": false
}
Binary file removed example/assets/images/sd_1024568622.gif
Binary file not shown.
Binary file removed example/assets/images/sd_165689209.gif
Binary file not shown.
Binary file removed example/assets/images/sd_968081805.gif
Binary file not shown.
6 changes: 3 additions & 3 deletions example/assets/slides.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"background": "assets/images/sd_1024568622.gif",
"background": "https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExc21yZzZhNzQ3bmt4dGk3amE5a2ozaHQxbTdpeGM4bHlmazdibmJjdSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/8L43c9x5Lvl2o/giphy.gif",
"style": "custom",
"transition": {
"type": "fade_in",
Expand Down Expand Up @@ -47,7 +47,7 @@
"layout": "image"
},
{
"background": "assets/images/sd_968081805.gif",
"background": "https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExZGt1MnQ5N2k3cXVma24wb3V5cThlZ3ExY2NvY3czcmozang0bGQ1ZSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/XzWd8acQ37byKR4tmd/giphy.gif",
"style": "cover",
"transition": {
"type": "fade_in",
Expand Down Expand Up @@ -157,7 +157,7 @@
"layout": "two_column"
},
{
"background": "assets/images/sd_165689209.gif",
"background": "https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExeGswdWJvY2oxazJoY3g2Y2poNHBvZXlpYmd5YTg0Z2g0ODRrbng4MyZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/oB6KlAvOuaLtxYy8l4/giphy.gif",
"style": "cover",
"transition": {
"type": "fade_in",
Expand Down
47 changes: 19 additions & 28 deletions lib/components/molecules/slide_preview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,28 @@ class SlidePreview extends StatelessWidget {

@override
Widget build(BuildContext context) {
return LayoutBuilder(builder: (context, constraints) {
final sideWidth = SplitViewProvider.sideWidthOf(context);
final paddingSize = sideWidth / 20;
final size = SplitViewProvider.sizeOf(context);
final sideWidth = SplitViewProvider.sideWidthOf(context);
final paddingSize = sideWidth / 20;

final rightWidth = size.width - sideWidth;
final rightHeight = size.height * (rightWidth / size.width);
final rightSize = Size(rightWidth - (paddingSize * 2), rightHeight);
return Container(
color: const Color.fromARGB(144, 0, 0, 0),
child: Align(
alignment: Alignment.center,
child: ConstrainedBox(
constraints: BoxConstraints.tight(rightSize),
child: Container(
margin: EdgeInsets.all(paddingSize),
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.3),
blurRadius: 6,
spreadRadius: 3,
),
],
),
child: SlideView(slide),
return Container(
color: const Color.fromARGB(144, 0, 0, 0),
alignment: Alignment.center,
child: Container(
margin: EdgeInsets.all(paddingSize),
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.3),
blurRadius: 6,
spreadRadius: 3,
),
),
],
),
);
});
child: SlideConstraintBuilder(builder: (context, _) {
return SlideView(slide);
}),
),
);
}
}

Expand Down
5 changes: 2 additions & 3 deletions lib/helpers/loader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ class SlidesLoader {
}

if (kDebugMode) {
await generate();
return _loadFromLocalStorage();
} else {
return _loadFromRootBundle();
Expand All @@ -90,7 +89,7 @@ Future<DeckData> _loadFromLocalStorage() async {
return (
slides: _parseFromJson(slidesJson),
assets: _parseAssets(assetsJson),
config: Config.fromJson(configJson),
config: ProjectConfig.fromJson(configJson),
);
}

Expand All @@ -105,7 +104,7 @@ Future<DeckData> _loadFromRootBundle() async {
return (
slides: _parseFromJson(slidesJson),
assets: _parseAssets(assetsJson),
config: Config.fromJson(configJson)
config: ProjectConfig.fromJson(configJson)
);
}

Expand Down
56 changes: 34 additions & 22 deletions lib/helpers/markdown_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ final _mermaidBlockRegex = RegExp(r'```mermaid([\s\S]*?)```');
typedef DeckData = ({
List<Slide> slides,
List<SlideAsset> assets,
Config config,
ProjectConfig config,
});

final emptyDeckData = (
slides: <Slide>[],
assets: <SlideAsset>[],
config: const Config.empty(),
config: const ProjectConfig.empty(),
);

typedef MarkdownData = ({
typedef ProcessData = ({
String content,
Map<String, dynamic> options,
Config config,
ProjectConfig config,
});

class Pipeline {
Expand All @@ -54,6 +54,8 @@ class Pipeline {
}
}
}

assetsLoaded.clear();
}

const Pipeline({
Expand All @@ -64,13 +66,16 @@ class Pipeline {
static Future<SlideAsset?> getAsset(String fileName) async {
final assetFile = SlideAsset.buildFile(fileName);

return await SlideAsset.maybeLoad(assetFile);
final asset = await SlideAsset.maybeLoad(assetFile);
if (asset != null) {
Pipeline.assetsLoaded.add(asset);
}
return asset;
}

static Future<SlideAsset> saveAsset(
String fileName, {
required List<int> data,
required,
}) async {
final imageFile = SlideAsset.buildFile(fileName);

Expand All @@ -84,7 +89,7 @@ class Pipeline {
Future<DeckData> run(String contents) async {
final slidesRaw = _splitSlides(contents.trim());

final config = await _loadConfig();
final config = await _loadProjectConfig();

final slides = <Slide>[];

Expand All @@ -100,7 +105,10 @@ class Pipeline {
config: config,
);

return await _runPostMarkdown(data);
final result = await _runPostMarkdown(data);

await cleanAssets();
return result;
}

Future<DeckData> _runPostMarkdown(DeckData data) async {
Expand All @@ -113,8 +121,8 @@ class Pipeline {
return updatedData;
}

Future<Slide> runEach(String slideContents, Config config) async {
MarkdownData result = (content: slideContents, options: {}, config: config);
Future<Slide> runEach(String slideContents, ProjectConfig config) async {
ProcessData result = (content: slideContents, options: {}, config: config);

for (final processor in markdown) {
result = await processor.run(result);
Expand All @@ -129,15 +137,15 @@ class Pipeline {
});
}

Future<Config> _loadConfig() async {
Future<ProjectConfig> _loadProjectConfig() async {
final file = kConfig.projectConfigFile;

if (!await file.exists()) {
return const Config.empty();
return const ProjectConfig.empty();
}

final configContents = await file.readAsString();
return Config.fromYaml(configContents);
return ProjectConfig.fromYaml(configContents);
}

Future<Slide> _parseSlideFromMap(Map<String, dynamic> slideMap) async {
Expand Down Expand Up @@ -296,7 +304,7 @@ class StoreLocalReferencesProcessor extends PostMarkdownProcessor {
abstract class MarkdownProcessor {
const MarkdownProcessor();

FutureOr<MarkdownData> run(MarkdownData data);
FutureOr<ProcessData> run(ProcessData data);
}

final _frontMatterRegex = RegExp(r'---([\s\S]*?)---');
Expand All @@ -305,7 +313,7 @@ class FrontMatterProcessor extends MarkdownProcessor {
const FrontMatterProcessor();

@override
MarkdownData run(MarkdownData data) {
ProcessData run(ProcessData data) {
final frontMatter =
_frontMatterRegex.firstMatch(data.content)?.group(1) ?? '';

Expand All @@ -321,7 +329,7 @@ class FrontMatterProcessor extends MarkdownProcessor {
options['raw'] = frontMatter;

final mergedOptions = deepMerge(
data.config.toMap(),
data.config.toSlideMap(),
options,
);

Expand All @@ -337,7 +345,11 @@ class ImageMarkdownProcessor extends MarkdownProcessor {
const ImageMarkdownProcessor();

@override
Future<MarkdownData> run(MarkdownData data) async {
Future<ProcessData> run(ProcessData data) async {
// Do not cache remot edata if cacheRemoteAssets is false
if (!data.config.cacheRemoteAssets) {
return data;
}
// Get any url of images that are in the markdown
// Save it the local path on the device
// and replace the url with the local path
Expand All @@ -352,7 +364,7 @@ class ImageMarkdownProcessor extends MarkdownProcessor {
final imageUrl = match.group(1);
if (imageUrl == null) continue;

final asset = await _fetchAsset(imageUrl);
final asset = await _cacheRemoteAsset(imageUrl);

if (asset != null) {
final imageMarkdown = '![Image](${asset.relativePath})';
Expand All @@ -365,7 +377,7 @@ class ImageMarkdownProcessor extends MarkdownProcessor {
var background = options['background'];

if (background != null && background is String) {
final asset = await _fetchAsset(background);
final asset = await _cacheRemoteAsset(background);

if (asset != null) {
background = asset.relativePath;
Expand All @@ -376,7 +388,7 @@ class ImageMarkdownProcessor extends MarkdownProcessor {
var imageSource = options['options']?['src'];

if (imageSource != null && imageSource is String) {
final asset = await _fetchAsset(imageSource);
final asset = await _cacheRemoteAsset(imageSource);

if (asset != null) {
imageSource = asset.relativePath;
Expand All @@ -391,7 +403,7 @@ class ImageMarkdownProcessor extends MarkdownProcessor {
);
}

Future<SlideAsset?> _fetchAsset(String url) async {
Future<SlideAsset?> _cacheRemoteAsset(String url) async {
if (!url.startsWith('http')) {
return null;
}
Expand Down Expand Up @@ -427,7 +439,7 @@ class MermaidProcessor extends MarkdownProcessor {
const MermaidProcessor();

@override
Future<MarkdownData> run(MarkdownData data) async {
Future<ProcessData> run(ProcessData data) async {
final replacements = <Replacement>[];

var content = data.content;
Expand Down
Loading

0 comments on commit 09c7bf8

Please sign in to comment.