@@ -1292,6 +1292,8 @@ List<Widget> _buildTemplateMenuChildren(
12921292 final isDefault = settings.defaultTemplate == path;
12931293 final template = file.data! ;
12941294 final metadata = template.getMetadata ()! ;
1295+ final templateBackgrounds =
1296+ template.getPage ()? .backgrounds ?? const < Background > [];
12951297
12961298 return [
12971299 if (! isCore && fileSystem.storage == null )
@@ -1311,6 +1313,33 @@ List<Widget> _buildTemplateMenuChildren(
13111313 _overrideTools (fileSystem, bloc, [file]);
13121314 },
13131315 ),
1316+ if (bloc != null && templateBackgrounds.isNotEmpty)
1317+ SubmenuButton (
1318+ leadingIcon: const PhosphorIcon (PhosphorIconsLight .image),
1319+ menuChildren: [
1320+ MenuItemButton (
1321+ leadingIcon: const PhosphorIcon (PhosphorIconsLight .file),
1322+ child: Text (AppLocalizations .of (context).currentPage),
1323+ onPressed: () {
1324+ _applyTemplateBackgroundsToPages (bloc, template, [null ]);
1325+ },
1326+ ),
1327+ MenuItemButton (
1328+ leadingIcon: const PhosphorIcon (PhosphorIconsLight .files),
1329+ child: Text (AppLocalizations .of (context).allPages),
1330+ onPressed: () {
1331+ final state = bloc.state;
1332+ if (state is ! DocumentLoadSuccess ) return ;
1333+ _applyTemplateBackgroundsToPages (
1334+ bloc,
1335+ template,
1336+ state.data.getPages (true ),
1337+ );
1338+ },
1339+ ),
1340+ ],
1341+ child: Text (AppLocalizations .of (context).applyBackground),
1342+ ),
13141343 MenuItemButton (
13151344 leadingIcon: const PhosphorIcon (PhosphorIconsLight .copy),
13161345 child: Text (AppLocalizations .of (context).duplicate),
@@ -1372,3 +1401,37 @@ List<Widget> _buildTemplateMenuChildren(
13721401 ),
13731402 ];
13741403}
1404+
1405+ void _applyTemplateBackgroundsToPages (
1406+ DocumentBloc bloc,
1407+ NoteData template,
1408+ List <String ?> pageNames,
1409+ ) {
1410+ final state = bloc.state;
1411+ if (state is ! DocumentLoadSuccess ) return ;
1412+ final backgrounds = template.getPage ()? .backgrounds ?? const < Background > [];
1413+ for (final background in backgrounds.whereType <SourcedElement >()) {
1414+ final uri = Uri .tryParse (background.source);
1415+ if (uri? .scheme.isNotEmpty ?? true ) continue ;
1416+ final asset = template.getAsset (background.source);
1417+ if (asset != null ) {
1418+ bloc.add (AssetUpdated (background.source, asset));
1419+ }
1420+ }
1421+ final originalPageName = state.pageName;
1422+ var activePageName = originalPageName;
1423+ for (final pageName in pageNames) {
1424+ if (pageName == null ) {
1425+ bloc.add (DocumentBackgroundsChanged (backgrounds));
1426+ continue ;
1427+ }
1428+ if (pageName != activePageName) {
1429+ bloc.add (PageChanged (pageName));
1430+ activePageName = pageName;
1431+ }
1432+ bloc.add (DocumentBackgroundsChanged (backgrounds));
1433+ }
1434+ if (activePageName != originalPageName) {
1435+ bloc.add (PageChanged (originalPageName));
1436+ }
1437+ }
0 commit comments